What will be the value of y after executing the following statements:

int x = Integer.MIN_VALUE;
int y = -x;
When applied to x unary operator - returns a value of 0-x. But value y=2147483648 (which is 0b10000000000000000000000000000000 ) will not fit into the range of type int as a positive value: due to it's first bit it will be considered as a negative value, and will be equal to x. Therefore, y=-x, y=x --> x=-x

I believe it'd be a good idea to write in explanation the limits of int

2023 Feb 17, 6:02:46 AM

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Sign Up Now
or Subscribe for future quizzes