I found an interesting fact, but I don't know how it happened.
Integer x = 10;
Integer y = 10;
System.out.print(x==y); // true
Integer x = 128;
Integer y = 128;
System.out.print(x==y); // false
I found an interesting fact, but I don't know how it happened.
Integer x = 10;
Integer y = 10;
System.out.print(x==y); // true
Integer x = 128;
Integer y = 128;
System.out.print(x==y); // false
Integer
comparison using ==
works only for numbers between -128 and 127.
Because Integer
is an Object
, use equals
that will work for all values.