• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing objects using ==operator.  RSS feed

 
midhuna peru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • 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


 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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".
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • 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
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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).
 
Ruud Smith
Greenhorn
Posts: 11
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Got it guys. Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!