GeeCON Prague 2014*
The moose likes Java in General and the fly likes Should I use Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Should I use "equal" or "==" in this case?" Watch "Should I use "equal" or "==" in this case?" New topic
Author

Should I use "equal" or "==" in this case?

Chrix Wu
Ranch Hand

Joined: Nov 15, 2009
Posts: 121
Hi folks,

I am doing unit test on an getInstance() method of a singleton class.

I want to test two getInstance() calls are actually returning the same object.

Should i use equal or '==' to compare the two references?

And what are their differences?


** SCJP 5.0 84% **
** SCWCD 1.5 76% **
Chrix Wu
Ranch Hand

Joined: Nov 15, 2009
Posts: 121
Is "equal" method actually compare the hashcode of two objects in order to test their equality?
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

if you want to compare the two refernce to check whether they are equal or not then use ==.

but when you want to know whether the two objects are meaningfully equivalent then use equals.

in your case as you want to know whether two objects are equal or not then i recommend use equals


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

and also if two hashcode are equals then it doesnot means that object will be equal
it means they are in same bucket
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
For singleton instance '==' is what you should be using, since in case of objects it compares their location in memory.


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Chrix Wu
Ranch Hand

Joined: Nov 15, 2009
Posts: 121
Thank you all, folks.

By the way, Shanky Sohar, when you said "use equals to check whether two objects are meaningfully equal?"

what does that mean?

In other words, what is the inner implementation under equals method?
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
It depends on programmers opinion, or the innerworks of the program. For example if you mada a person class with all kinds of attributes, from name, height, age, skin tone, eye color, IQ etc. your equals method might take all of them into consideration or just name and surname.
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

== will work here, the equals method won't, and I'll tell you why:

== checks for identity, meaning it will only return true if the objects compared have the same address in memory

equals() checks for meaningful equivalency, meaning it will return true whenever the equals() method returns true. Those conditions depend on which implementation of equals() the developer of the first class you're comparing has used. There are rules for creating an appropriate equals class, but you don't always have a guarantee that the implemented equals() method is appropriate. Even if it were appropriate, it would probably only check if the contents are equal, which, if your method "getInstance()" say, returns a new instance for every call, would also result in true, which is definitely not what you want.

which means if you want to make sure it is returning the same item, and not an equivalent other instance, you must use ==.

I hope that is sufficient explanation.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Chrix Wu
Ranch Hand

Joined: Nov 15, 2009
Posts: 121
Dieter Quickfend wrote:== will work here, the equals method won't, and I'll tell you why:

== checks for identity, meaning it will only return true if the objects compared have the same address in memory

equals() checks for meaningful equivalency, meaning it will return true whenever the equals() method returns true. Those conditions depend on which implementation of equals() the developer of the first class you're comparing has used. There are rules for creating an appropriate equals class, but you don't always have a guarantee that the implemented equals() method is appropriate. Even if it were appropriate, it would probably only check if the contents are equal, which, if your method "getInstance()" say, returns a new instance for every call, would also result in true, which is definitely not what you want.

which means if you want to make sure it is returning the same item, and not an equivalent other instance, you must use ==.

I hope that is sufficient explanation.


Martin Vanyavchich wrote:It depends on programmers opinion, or the innerworks of the program. For example if you mada a person class with all kinds of attributes, from name, height, age, skin tone, eye color, IQ etc. your equals method might take all of them into consideration or just name and surname.


Ok, i see, we should override equals() in order to give it more meaningful object comparision.

BUT what if i dont override the equals() method in my own class, what is the equals() actually doing?

What is it doing in order to decide to return true or false?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39062
    
  23
. . . and the equals() method in the class you are using should have documentation comments to describe how it works.
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
JavaDoc wrote:The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
You can read the whole thing here

In my opinion you don't need an equals method for a Singleton class, since if it is properly implemented '==' should always return true.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

If it's a JUnit test we're talking about why not simply use Assert.assertSame()?


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I was going to say that.
Chrix Wu
Ranch Hand

Joined: Nov 15, 2009
Posts: 121
Thanks everybody.

I learned a lot
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39062
    
  23
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Should I use "equal" or "==" in this case?