• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between equals() and == for Objects?  RSS feed

 
Shane Jensen
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

As far as I understand, equals() compares the actual value for Strings, while == checks whether two Strings refer to the same reference object.

However, for other Objects, equals() and == seem to be doing the exact same thing; they both check whether two Objects refer to the same reference variable. I just can't see the difference. Can anyone explain this to me as simple as possible?

Thank you in advance!

Regards,
Shane
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37395
531
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shane,
The Object class implements equals() is a way that is equivalent to ==. So if you have the Object class, it definitely behaves the same.

Subclasses of Object, have the option of overriding the behavior of equals(). String is one such class that does. There are many others that provide custom equals() methods as well. And as you noted, many that do not and inherit the == logic.
 
Shane Jensen
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Shane,
The Object class implements equals() is a way that is equivalent to ==. So if you have the Object class, it definitely behaves the same.

Subclasses of Object, have the option of overriding the behavior of equals(). String is one such class that does. There are many others that provide custom equals() methods as well. And as you noted, many that do not and inherit the == logic.

Hi Jeanne,

Thanks for your quick reply. So just for complete clarity: for classes that don't override equals() (unlike String), the behavior and thus the result of equals() and == will be exactly the same?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37395
531
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shane Jensen wrote:So just for complete clarity: for classes that don't override equals() (unlike String), the behavior and thus the result of equals() and == will be exactly the same?

Yes. In the JDK I have on my machine, the implementation in the Object class is


As you can see, it calls == which shows it is *exactly* the same!
 
Shane Jensen
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:
Shane Jensen wrote:So just for complete clarity: for classes that don't override equals() (unlike String), the behavior and thus the result of equals() and == will be exactly the same?

Yes. In the JDK I have on my machine, the implementation in the Object class is


As you can see, it calls == which shows it is *exactly* the same!

Alright then. Thanks
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:
Shane Jensen wrote:So just for complete clarity: for classes that don't override equals() (unlike String), the behavior and thus the result of equals() and == will be exactly the same?

Yes.

I have to disagree!

Although the equals() method of the Object class is implemented using the == operator, there are two major differences between equals() and ==, namely:
1/ if you invoke equals() on a null reference, you'll get a NullPointerException; using == you will never get a NullPointerException. Illustrated in this code snippet

2/ if you use == with incompatible types, you'll get a compiler error; using equals() you'll never get a compiler error. Again illustrated in the following code snippet

Hope it helps!
Kind regards,
Roel
 
Tobias Bachert
Ranch Hand
Posts: 86
18
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The a==2 should be a==(Integer) 2 (or a==Integer.valueOf(2)), otherwise the examples won't show the desired behavior.
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tobias Bachert wrote:The a==2 should be a==(Integer) 2 (or a==Integer.valueOf(2)), otherwise the examples won't show the desired behavior.

You are definitely correct about the first code snippet Have a cow! Without using Integer.valueOf(2) the value of a will be unboxed to compare two int primitives resulting in a NullPointerException as well. For the second code snippet I added it as well for consistency, although it's less needed as it only needs to show that == can result in a compiler error (and equals() won't).
 
Shane Jensen
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Roel and Tobias for the clarifications.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Posts: 3015
236
Android Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations Shane Jensen,

Your question has made it to our Journal   

Have a Cow!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!