• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals() method and its significance

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

The java docs say that:

public boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

What is meant by o1 being "equal" to o2? Can someone explain with a simple example please?
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:Hi all

The java docs say that:

public boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

What is meant by o1 being "equal" to o2? Can someone explain with a simple example please?


The concept of what makes one Object equal to another is left to the Class designer to define. The JavaDocs tell you what the Object class design defined, and the rules for overriding the method. If your class wants to have different rules to indicate that two instances are equal you would override the method (following the contract in the docs.)
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say you got:

Yout then create 3 Objects like

If you now would call p1.equals(p2) it would return false because they are not the same object even if the object represents the same thing.
To make this work you have to override the default equals method with your own.
This can be really tricky and can lead to weird behaviour for example in HashSet/HashMap.
Most IDEs have build in builders which construct an equals method but I would use them as a starting point because they might not really reflect your usecase.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manuel Petermann wrote:Lets say you got:

Yout then create 3 Objects like

If you now would call p1.equals(p2) it would return false because they are not the same object even if the object represents the same thing.
To make this work you have to override the default equals method with your own.
This can be really tricky and can lead to weird behaviour for example in HashSet/HashMap.
Most IDEs have build in builders which construct an equals method but I would use them as a starting point because they might not really reflect your usecase.


So, in essence, what you are saying is that tho objects are meaningfully equivalent depending on how I (the programmer ) wants them to be considered equal or if they refer to the same object on a heap. There is no hard and fast
rule. Correct?
 
Winston Gutkowski
Bartender
Pie
Posts: 10272
60
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:So, in essence, what you are saying is that tho objects are meaningfully equivalent depending on how I (the programmer ) wants them to be considered equal or if they refer to the same object on a heap. There is no hard and fast rule. Correct?

Correct. However, you'd better make it a good choice, because it's tied to the class definition, and clients may rely on what it does, so you can't just change it willy-nilly.

That's what Comparator's are for.

Winston
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Mansukhdeep Thind wrote:So, in essence, what you are saying is that tho objects are meaningfully equivalent depending on how I (the programmer ) wants them to be considered equal or if they refer to the same object on a heap. There is no hard and fast rule. Correct?

Correct. However, you'd better make it a good choice, because it's tied to the class definition, and clients may rely on what it does, so you can't just change it willy-nilly.

That's what Comparator's are for.

Winston


Thanks Winston..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic