Is It Possible for a number that is greater than Integer.MAX_VALUE for both Integers?

-3
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
asked on Stack Overflow May 31, 2018 by Ashok Mutyala

2 Answers

0

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.

answered on Stack Overflow May 31, 2018 by codeo
0

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.

answered on Stack Overflow May 31, 2018 by Gino Mempin • edited May 31, 2018 by Gino Mempin

User contributions licensed under CC BY-SA 3.0