0

I understand that int values, when autoboxed, are interned i.e no 2 objects containing the same int are present, and so we can compare two Integers by == So why does the following happen :

Integer iRef1=1000;
Integer iRef2=1000;
System.out.println(iRef1 == iRef2);
System.out.println(iRef1.equals(iRef2));

This prints

false
true

On the other hand, if I say

Integer iRef1=10;
Integer iRef2=10;
System.out.println(iRef1 == iRef2);
System.out.println(iRef1.equals(iRef2));

the output is

true
true

Why is there no interning in the second case ?

Edited by daudiam: n/a

1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by daudiam
0

Sorry, I failed to notice that INTs are interned only when they are in the range of -128 to 127

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.