• 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

Comparing objects using ==operator.

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys!
I was reading about equals() in Complete Reference and i got doubt in this sentence.
"The equals( ) method compares the characters inside a String object. The == operator compares two object references to see whether they refer to the same instance."
I understood equals() but din't get == operator. I tried this program, please explain.

str2==str is false. why? how declaring str2 is different from str3.
if I change str1="he", only (str3==str) is true. why?


Output


 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This topic has been discussed many, many times here.

In a nutshell, your variables hold (more or less) the address in memory of the object, not the object itself. But to complicate the matter, Strings are treated different than other objects (you may want to search for "string pool").

when you do this:

the string "Hello" is created in the String pool, and the variable str is set to point to that. Then you get to

Java says "well, that "Hello" string is already in the string pool, so I'll have this new variable point to that same spot."
Then we get to this:

EVERY time you use the 'new' operator, you create a new object in memory, in a new memory location. In this case, we now create a brand new String object, give it the value of "Hello", and put its memory location in str2.

So, effectively, str and str1 are like pieces of paper both of which have "123 main street" written on them. If you go to that address, whether you read it off paper slip 1 or paper slip 2, you end up at the same spot, and see the same house.

However, when you said "new String(str)", you said "build a new house somewhere that looks exactly like the one you find at str". so str2 may have an address of "789 elm street". The two houses are identical in every way, but they are two distinct houses.


equals says "do the houses found at these addresses look exactly the same". == says "do the paper slips have the same address written on them".
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because when you create Strings using str1="Hello" the JVM is clever enough to spot that that exact same string has already been created and as String objects are immutable it can safely assign the same String object to str and str1. But when you use the String constructor to create a String ie str2=new String(str) you are saying you want a new object with the exact same contents as str.

So str and str1 reference the same object whereas str2 references a different object but with the same value as str. Therefore, they will all be equal to each other when using the equals() method as this checks the contents but when using == which checks if the object are the same only str and str1 will be considered equal.

Edit: Doh!! too late
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to what Fred already explained there's a nice little how-to page on when you should avoid the equality operator (AvoidTheEqualityOperator).
 
Greenhorn
Posts: 11
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've never heard of a String Pool before, but after a small search I've found a good explanation here.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It all comes down to this: The == operator compares references, not objects' contents. So if == evaluates to true, then both references point to the same object, or both are null. If these are the semantics you want (which is usually NOT the case), use ==. If you want to compare the states (contents) of 2 objects, you must use the equals() method, not the == operator.

If == sometimes gives true when you didn't think it would, then it's because you were assuming that two references didn't point to the same object, when in fact they did. The main case where that pops up is the String constant pool, but there are also the caches for various wrapper classes, and the JVM or even Java methods that you're calling are free to reuse the same object in some cases where you might think they wouldn't. When you see an unexpected true for ==, it's not that == functionality has changed.
 
Ranch Hand
Posts: 933
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>>equals says "do the houses found at these addresses look exactly the same". == says "do the paper slips have the same address written on them".


Above is most simplest and clear explanation I ever read on this confusing topic. Thank you.
 
midhuna peru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it guys. Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic