wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes equals() method and its significance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals() method and its significance" Watch "equals() method and its significance" New topic
Author

equals() method and its significance

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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?


~ Mansukh
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.)


Steve
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

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.


Please correct my English.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Mar 17, 2011
Posts: 8244
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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..
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: equals() method and its significance