Original question
Integer i1 = 1000;
Integer i2 = 1000;
if(i1 != i2) System.out.println("different objects");
if(i1.equals(i2)) System.out.println("meaningfully equal");
Produces the output:
different objects
meaningfully equal
Hi
in the first if statement i1 and i2 are compared as refrences and in second if statement values are compared.
In integer wrapper class the integers which are less than 128 are compared as values using (==) and any values after 128 are compared as refrences
class IntegerTest
{
public static void main(
String[]args)
{
Integer i1=127;
Integer i2=127;
if(i1 ==i2)
System.out.print("same objects");
if(i1.equals(i2))
System.out.println("equally");
}
};
and
class IntegerTest
{
public static void main(String[]args)
{
Integer i1=128;
Integer i2=128;
if(i1 ==i2)
System.out.print("same objects");
if(i1.equals(i2))
System.out.println("equally");
}
};
Hope this should be clear