I have some confusion regarding below program. here I got output as 128.
int i=0;
while(Integer.valueOf(i)==Integer.valueOf(i))
i++;
System.out.println(i);
output: 128
I have some confusion regarding below program. here I got output as 128.
int i=0;
while(Integer.valueOf(i)==Integer.valueOf(i))
i++;
System.out.println(i);
output: 128
The JVM caches int
values between -128 and 127 for the Integer
class. Therefore, the following statement will return true upto 127 :
Integer.valueOf(i)==Integer.valueOf(i)
Beyond 127, valueOf
returns a new Integer
object by default.
On the last iteration of your while
loop, i
will be 127 and i++
will change it to 128. That's why the output is 128 and not 127.