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
Зарегистрируйся сейчас
или Подпишись на будущие тесты