Mukesh Mukesh - 2 months ago 18
Java Question

Integer comparison confusion

Can anyone explain why the o/p is like this for the code below:

public static void main(String[] args) {

Integer i1=127;
Integer i2=127;
Integer i3=128;
Integer i4=128;

System.out.println(i1==i2);
System.out.println(i3==i4);
}


O/p

true
false

Answer

Values from -128 to 127 for int are cached for boxing. That's why the first comparison returns true.

http://www.mohawksoft.org/?q=node/70

Comments