If we have expressions something like the following,
Integer x=500;
Integer y=500;
System.out.println(x==y);
then, the result of the operation x==y
will be false
because we are comparing the object references.
If I have a loop for these two variables x
and y
something like the following,
Integer x=0;
Integer y=0;
for(int i=0;i<2000;i++)
{
System.out.println((x==y)+" : "+x+++" : "+y++);
}
then, it displays true
until the value of both x
and y
is 127
. In all other cases (i.e when the value of x
and y
is incremented over 127
- when the value of these variables is greater than 127
).
So why does this happen? Is there any specification for this behaviour?