Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

how is this possible please explain

 
Ranch Hand
Posts: 110
Android Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Integer i1=1000;

Integer i2=1000;

if(i1!=i2) System.out.println("different objects");

if(i1.equals(i2)) System.out.println("meaningfully equal");


output

different objects //how is this possible please explain

meaningfully equal









b]Integer i3=10;

Integer i4=10;

if(i3==i4) System.out.println("same objects");

if(i3.equals(i4)) System.out.println("meaningfully equal");


output

same objects //how is this possible please explain

meaningfully equal


how if(i3==i4) and if(i1!=i2) both can be true
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
When you initialize an Integer object using a literal value the auto-boxing mechanism uses Integer.valueOf() under the covers.
This method first checks a cache of Integer objects, which contains instances for primitive values -128 up to 127 by default.
If the primitive value passed to the valueOf() method lies within that range, a reference to the cached object is returned, otherwise a new Integer wrapper instance will be created (using the constructor).

In the case of you scenario the valueOf() call made by the auto-boxing mechanism will result in the creation of new Integer objects for i1 and i2, because the cache doesn't contain instance for that particular value. In case of i3 and i4 a cached Integer reference will be assigned. So i1 and i2 will refer to distinct Integer objects to which referential equality doesn't apply, whereas i3 and i4 will refer to the same object, to which referential equality does apply. If you are interested in "meanifull equivalency" you should use the equals() method to compare the Integer objects, and not the == or != operators.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Shashank:

This is possible because Integer has a cache of statically created objects. The minimum range is -128 - 127, but the actual range is based on where you get your libraries from.

John.
 
Sheriff
Posts: 22702
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please Use One Thread Per Question. This question is the same as this one. Closing this thread.
 
Forget Steve. Look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic