What happens when you try to compile and run the following class... public class TestClass{ public static void main(String[] args) throws Exception{ int a = Integer.MIN_VALUE; int b = -a; System.out.println( a+ " "+b); } }
Output?
Your comments please... with explanations.
I am not a player, I am the 'GAME'
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
posted
0
What happened when you compiled and ran the code? What do you not understand about the output? [ November 18, 2006: Message edited by: Barry Gaunt ]
What happened when you compiled and ran the code? What do you not understand about the output?
Well... the output did not make sense to me. The output is -2147483648 -2147483648
Any idea?
Sanjeev Singh
Ranch Hand
Joined: Nov 01, 2006
Posts: 381
posted
0
Printing Integer.MIN_VALUE will give you -2147483648 and again negating this number will give +2147483648 (out of the range from Integer.MAX_VALUE i.e. equals to Integer.MAX_VALUE+1).Doing the 2's compliment airithmetic to fit this number in 32 bit scheme ,you will get the answer as -2147483648.