• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Nil. Hatamova
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the output is difference for equals() method?


Arent the equals() method same?
The output is:
false
false
true
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 22096
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 6503
83
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic