Forums Register Login

Integer.MIN_VALUE

+Pie Number of slices to send: Send
Consider the following code from jqplus6:


public class TestClass
{
public static void main(String[] args) throws Exception
{
int a = Integer.MIN_VALUE;
int b = -a;
System.out.println( a+ " "+b);
}
}




It will print two same negative numbers.

Why???
+Pie Number of slices to send: Send
because +2147483648 is out of range for Int primitive
+Pie Number of slices to send: Send
don't call me primitive!
+Pie Number of slices to send: Send
Ha ha ,
Classic example of mis-understanding.
i meant to say , 'Int primitive' NOT 'Int, primitive' (see comma) ...

primitive adjective was for Int and not for you.


V
+Pie Number of slices to send: Send
I see


But I don't understand how it come that because +2147483648 is out of range for Int primitive, the code I stated printed two negative numbers


Shouldn't we receive some kind of error or exception?
[ May 11, 2008: Message edited by: Ismael Upright ]
+Pie Number of slices to send: Send
because the max positive integer is not 2147483648 but 2147483647 because the 0 is also considered a positive int.
so if you have an int of 2147483648, it is out of range.
The couter of int goes:
2147483646
2147483647
-2147483648
-2147483647
+Pie Number of slices to send: Send
Ismael,

This is explicity addressed in the Java Language Specification for the Unary Minus Operator -:


For integer values, negation is the same as subtraction from zero. The Java programming language uses two's-complement representation for integers, and the range of two's-complement values is not symmetric, so negation of the maximum negative int or long results in that same maximum negative number. Overflow occurs in this case, but no exception is thrown. For all integer values x, -x equals (~x)+1.

 
+Pie Number of slices to send: Send
All clear now. Thenks guys for explaining that
I want my playground back. Here, I'll give you this tiny ad for it:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 815 times.
Similar Threads
Why a & b are same ?
Confused with the output
~ operator
Simple code strange output aleast for me
Math.abs()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:02:16.