int x = ?;
if(x > Integer.MAX_VALUE)
{
System.out.println(x);
}
when the above condition gets true??? what is the value of x? Integer.MAX_VALUE = 0x7fffffff, I tried x with 0x80000000 in hexa decimal. But x is now negative number.
Java integers are 32-bit, and anything above the maximum value for a 32-bit number will get rolled over and become negative. This is known as integer overflow.
If you have:
int x = Integer.MAX_VALUE;
x += 1;
x
will equal -2147483648
, or Integer.MIN_VALUE
.
There is no such x
.
From the Java docs:
int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -2^31 and a maximum value of 2^31-1.
That max 32-bit value is equal to 0111 1111 1111 1111 1111 1111 1111 1111
.
It's a 2's complement representation, so setting the 1st bit to 1 will result to a negative number (0x80000000 = 1000..0000). So, there's really no possible value for the x
you're looking for.
User contributions licensed under CC BY-SA 3.0