• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Wrapper ==, != (K&B SCJP 6)

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting very different and confusing results for == and != (different than what is explained on p 246)

When I execute :

Integer i3 = 10;
Integer i4 = 10;

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

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

if (i3.equals(i4)) System.out.println("meaningfully same");
else System.out.println("meaningfully diff");

the output is
same objects
same objects
meaningfully same

And the same code with following values gives different results

Integer i3 = 1000;
Integer i4 = 1000;

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

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

if (i3.equals(i4)) System.out.println("meaningfully same");
else System.out.println("meaningfully diff");

gives output

different objects
different objects
meaningfully same

-->Why is this happening?

--> How does == and != work? the book has different results and explanation (or am I missing something obvious???)
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats the magic of Integer pool.
Firstly == is used to check the references(address) of 2 objects whereas equals() method is used to check the content of 2 objects.
So when you are writing Here you are creating the object i1 and storing the value 10, that's fine. Now So you must be thinking that Java will create another object i2 with again the same value 10 then you are thinking wrong because Java maintains the Integer pool where it stores the value if they are in the range -128 to 127.
.
Now lets see what happens when you first created the object i1 with value 10 then Java puts this value in Integer pool(since 10 was within the above range) and when again you are creating another object with the same value 10 Java will not create object and just pass a reference to the already created object i1. SO the summary is i1 and i2 are same object therefore .
.
Then you wrote So i3 object is created with value 1000 and then you wrote this time even though
i4 has the same value as i3, it will be created (i.e. i4 object will be created) because as mentioned above the range is -128 to 127. Therefore .
 
Sajesh Adulkar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the explanation - it makes sense now
 
Greenhorn
Posts: 8
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
== compares just memory locations, in case of objects.
equals() just checks if two objects are meaningfully equal.

Just to save some memory, Java assigns all wrapper classes with same small values (-128 to 127), with the same address. And for larger values, different addresses.
As a result, using == for same small values, points to same location in the memory, and thus returning true. Using == with larger values, might not do so.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for good explanation !!!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic