What happens when you try to compile and run the following class...
public class Main{ public static void main (String [] args) throws Exception{ int a = Integer.MIN_VALUE; int b = -a; System .out.println ( a+ " "+b); } }
Select 1 option
Correct Option is : B
It prints: -2147483648 -2147483648
Negative integers are stored in 2's complement form (complement the bits and add 1).
For example: Integer 1 in binary is 00000000 00000000 00000000 00000001 (32 bits)
So - 1 in binary would be (complement the bits for 1 and add 1) :
Now, let's see what happens in this question:
a = Integer.MIN_VALUE = 10000000 00000000 00000000 00000000
To get -a, apply the above two steps:
So you got the exact same value that you started with!
You can see the binary form of an integer using Integer.toBinaryString (i) method.