The concept is clear that, if two Integer objects has the same value, in the range of -128 to 127, they both refer to the same object.
For Example:
class Wrapper
{
public static void main(
String[] args)
{
Integer i1=10;
Integer i2=10;
System.out.println(i1==i2);// prints true
}
}
But please explain why the following code behaviour changes like this.
class Wrapper
{
public static void main(String[] args)
{
Integer i1=new Integer(10);
Integer i2=10;
System.out.println(i1==i2);//Why does this prints false ?
//i1=10; System.out.println(i1==i2); // when this line uncommented, why it prints true?
}
}
Please explain, I think i am missing some logic here.