• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Difference between equals() and == for Objects?

 
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 40747
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 40747
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 86
18
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Roel and Tobias for the clarifications.
 
Creator of Enthuware JWS+ V6
Posts: 3395
312
Android Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congratulations Shane Jensen,

Your question has made it to our Journal    

Have a Cow!
 
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic