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

int x = Integer.MIN_VALUE;
int y = -x;
Explanation
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

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Продовжуйте вивчати
тести з Java
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести