• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Is there a difference between equals() for String and for Object?

 
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why the output is difference for equals() method?


Arent the equals() method same?
The output is:
false
false
true
 
Bartender
Posts: 15741
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You aren't comparing two calls to equals(). In the first case, you're calling b1 == b2, which is NOT the same as b1.equals(b2).

Even so, Book doesn't have it's equals() method overridden, so it doesn't matter whether the ISBN of two books are the same, b1 and b2 will never be equal if they refer to different instances.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nil. Hatamova wrote:Why the output is difference for equals() method?

Arent the equals() method same?
The output is:
false
false
true



The equals() method of the Object class will return true, if the two objects are the same object.

The equals() method of the String class will return true, if the value of the two string objects are the same.

And since you did not override the equals() method, the equals() method of your Book class will return true, if the two book objects are the same object (just like the Object class).

Henry
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You aren't comparing two calls to equals().


Sure he is! He compares the statement b1.equals(b2) with the statement s1.equals(s). In both cases you are comparing 2 different instances and the first one prints false and the latter one true.

Stephan van Hulst wrote:In the first case, you're calling b1 == b2, which is NOT the same as b1.equals(b2).


That's not completely true for the given Book class Because the given Book class doesn't override the equals(Object) method, it inherits the equals(Object) method from the Object class and this method checks for reference (value) equality. And that's exactly the same as b1 == b2. The only difference between both ways of comparison (with the given Book class) occurs when b1 is null: b1 == b2 will print false or true (depening on the value of b2), but b1.equals(b2) will always throw a NullPointerException
 
Roel De Nijs
Sheriff
Posts: 11606
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

Nil. Hatamova wrote:Why the output is difference for equals() method?


Because any class can override the equals(Object) method of the Object class. And the String class overrides this method and will return true if and only if the argument is not null and is a String object that represents the same sequence of characters as the current (this) object. Your Book class doesn't override this method, so the equals(Object) method from the Object class is inherited. And this method returns true if and only if the argument refers to the same object as the current (this) object. So the code looks likeA very similar question is asked in this topic. And many classes from the Java API override the equals(Object) method of the Object class to provide a more meaningful implementation than reference equality. For example all wrapper classes, ArrayList (see this topic for detailed information),...

Now let's see if you have understood all the above. It's time for a pop quiz question. Consider this code snippetWhen I execute this class using the Book class from your first post, it prints 6x false. Now it's up to you to make appropriate changes to the Book class so that when you execute the TestBook class, this output is produced:
false
true
true
false
false
true


Hope it helps!
Kind regards,
Roel
 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:Sure he is! He compares the statement b1.equals(b2) with the statement s1.equals(s). In both cases you are comparing 2 different instances and the first one prints false and the latter one true.


I completely missed that line! Sorry!

That's not completely true for the given Book class Because the given Book class doesn't override the equals(Object) method, it inherits the equals(Object) method from the Object class and this method checks for reference (value) equality. And that's exactly the same as b1 == b2. The only difference between both ways of comparison (with the given Book class) occurs when b1 is null: b1 == b2 will print false or true (depening on the value of b2), but b1.equals(b2) will always throw a NullPointerException


I feel I need to clarify what I said here. I mean writing b1 == b2 is NOT the same as writing b1.equals(b2), in that you can not interchange them without thinking about it carefully. I agree that their results are the same if Object.equals() is never overridden.
reply
    Bookmark Topic Watch Topic
  • New Topic