• Post Reply 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Boxing, == and equals

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


My doubt here is how do we get "different object" for i1 and i2 and "same object" for i3 and i4. Could someone please explain this ?
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I give this :



It doesnt enter the print statement.


When I give this :



It enters the print statment.

When I give this :


or this:



It enters the print statement

 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
always remember that
"==" determines whether the reference variables refer to the same object
i.e. if Type obj = new Type();
Type obj1;
obj1 = obj;

then both the reference variables refer to the same object Type(); isn't it?
now in our situation,
Integer i1 = 1000;
Integer i2 = 1000;
we have two different objects having two different reference variables, hence if (i1 != i2) then println statement gets executed

and remember that equals method checks for the content of the object
now here
the content is same i.e. 1000 and 1000
hence i1.equals(i2) is true hence the meaningfully equivalent gets executed

But there is something weird with java, the equality depends upon the range of values we are providing
Read and remember following carefully :

Two instances of the wrapper objects Boolean, Byte, Character from \u0000 to \u007f and short and integer from -128 to 127 will always be == when their primitive values are same

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Here i1 and i2 are created through Boxing and Integers created through boxing in the range -128 to 127 always pass the == test if their values are equal. Here i1 and i2 are greater than this range hence they pass != test. You make i1 and i2 smaller than this range and then
(i1!=i2) will return false
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the actual value of Integer is below 128 then it goes to literal pool like String literals but the values greater then 127 are always placed at heap. Whatever is at pool that will be shared by all the instances. so

will be placed on literal pool and will share the same copy. Hence == will be evaluated to true.

And this works for direct assignments only. Objects created using new Integer() are always placed at Heap.
 
Greenhorn
Posts: 21
Mac Scala VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a really funky topic. Where in the specification is this clarified?



And got:


The key difference must be that the autoboxing is setting the two reference to the same object in the second case; whereas, in the first case they are indeed two different objects (Duh!).
 
Kaspar Christenson
Greenhorn
Posts: 21
Mac Scala VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've answered my own question. In section "5.1.7 Boxing Conversion" of the specification is states:

If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic