• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

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

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Chrix Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is "equal" method actually compare the hashcode of two objects in order to test their equality?
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and also if two hashcode are equals then it doesnot means that object will be equal
it means they are in same bucket
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For singleton instance '==' is what you should be using, since in case of objects it compares their location in memory.
 
Chrix Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
== 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.
 
Chrix Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Marshal
Posts: 64690
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and the equals() method in the class you are using should have documentation comments to describe how it works.
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's a JUnit test we're talking about why not simply use Assert.assertSame()?
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going to say that.
 
Chrix Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody.

I learned a lot
 
Campbell Ritchie
Marshal
Posts: 64690
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!