Long story short ...
if there are two objects to compare against each other (no matter used by standard Java libraries in background or by themeselves) the object1.equals(object2) method should be used.
Why or when should equals() be overriden?
An object.equals() method should be overriden, i.e. if there is a single ID attribute, a combination of attributes or something else which should equals, ALTHOUGH memory addresses of two object differs.
Usually the memory addresses will be compared (with help of the object.hashCode() method). That means if you create two independent objects (with new()) of the same class with the same attributevalues those objects won't be equal. So you have to decide, are two objects equal by their attributes or by their adresses.
To give you a better example with output result work through the code listing below.
Usually the memory addresses will be compared (with help of the object.hashCode() method). That means if you create two independent objects (with new()) of the same class with the same attributevalues those objects won't be equal. So you have to decide, are two objects equal by their attributes or by their adresses.
Why or when should hashCode() be overriden?
The method hashCode() should always be overriden when equals() will be overriden!To give you a better example with output result work through the code listing below.
Code example
package tutorial;public class HashCodeEqualsTutorial {
int id;
String text;
HashCodeEqualsTutorial(int id, String text) {
this.id = id;
this.text = text;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((text == null) ? 0 : text.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
HashCodeEqualsTutorial other = (HashCodeEqualsTutorial) obj;
if (id != other.id) return false;
if (text == null) {
if (other.text != null) return false;
} else if (!text.equals(other.text)) return false;
return true;
}
@Override
public String toString() {
return this.getClass().getName() + " [id=" + id + ", text=" + text + " hashCode=" + this.hashCode() + "]\n";
}
static String reportIfEquals(HashCodeEqualsTutorial obj1, HashCodeEqualsTutorial obj2) {
if (obj1.equals(obj2))
{
return " = "
+ obj1.toString()
+ "\n = "
+ obj2.toString();
} else {
return "<> "
+ obj1.toString()
+ "\n<> "
+ obj2.toString();
}
}
public static void main(String[] args) {
HashCodeEqualsTutorial obj1 = new HashCodeEqualsTutorial(1, "ABC");
HashCodeEqualsTutorial obj2 = new HashCodeEqualsTutorial(1, "ABC");
System.out.println(reportIfEquals(obj1, obj2));
obj2.setText("DEF");
System.out.println(reportIfEquals(obj1, obj2));
obj2.setId(2);
System.out.println(reportIfEquals(obj1, obj2));
obj2.setText(obj1.getText());
obj2.setId(obj1.getId());
System.out.println(reportIfEquals(obj1, obj2));
}
}
/* https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html
* 2016-07-28
*
...
The equals() Method
The equals() method compares two objects for equality and returns true if they are equal. The equals() method provided in the Object class uses the identity operator (==) to determine whether two objects are equal. For primitive data types, this gives the correct result. For objects, however, it does not. The equals() method provided by Object tests whether the object references are equal—that is, if the objects compared are the exact same object.
To test whether two objects are equal in the sense of equivalency (containing the same information), you must override the equals() method. Here is an example of a Book class that overrides equals():
public class Book {
...
public boolean equals(Object obj) {
if (obj instanceof Book)
return ISBN.equals((Book)obj.getISBN());
else
return false;
}
}
Consider this code that tests two instances of the Book class for equality:
// Swing Tutorial, 2nd edition
Book firstBook = new Book("0201914670");
Book secondBook = new Book("0201914670");
if (firstBook.equals(secondBook)) {
System.out.println("objects are equal");
} else {
System.out.println("objects are not equal");
}
This program displays objects are equal even though firstBook and secondBook reference two distinct objects. They are considered equal because the objects compared contain the same ISBN number.
You should always override the equals() method if the identity operator is not appropriate for your class.
Note: If you override equals(), you must override hashCode() as well.
...
...
The hashCode() Method
The value returned by hashCode() is the object's hash code, which is the object's memory address in hexadecimal.
By definition, if two objects are equal, their hash code must also be equal. If you override the equals() method, you change the way two objects are equated and Object's implementation of hashCode() is no longer valid. Therefore, if you override the equals() method, you must also override the hashCode() method as well.
The toString() Method
You should always consider overriding the toString() method in your classes.
The Object's toString() method returns a String representation of the object, which is very useful for debugging. The String representation for an object depends entirely on the object, which is why you need to override toString() in your classes.
You can use toString() along with System.out.println() to display a text representation of an object, such as an instance of Book:
System.out.println(firstBook.toString());
which would, for a properly overridden toString() method, print something useful, like this:
ISBN: 0201914670; The Swing Tutorial; A Guide to Constructing GUIs, 2nd Edition
« Previous • Trail • Next »
Your use of this page and all the material on pages under "The Java Tutorials" banner is subject to these legal notices.
Copyright © 1995, 2015 Oracle and/or its affiliates. All rights reserved.
...
*/
Code result
As output result (hashCode number will be different to yours).:= tutorial.HashCodeEqualsTutorial [id=1, text=ABC hashCode=65570]
= tutorial.HashCodeEqualsTutorial [id=1, text=ABC hashCode=65570]
<> tutorial.HashCodeEqualsTutorial [id=1, text=ABC hashCode=65570]
<> tutorial.HashCodeEqualsTutorial [id=1, text=DEF hashCode=68549]
<> tutorial.HashCodeEqualsTutorial [id=1, text=ABC hashCode=65570]
<> tutorial.HashCodeEqualsTutorial [id=2, text=DEF hashCode=68580]
= tutorial.HashCodeEqualsTutorial [id=1, text=ABC hashCode=65570]
= tutorial.HashCodeEqualsTutorial [id=1, text=ABC hashCode=65570]
Keine Kommentare:
Kommentar veröffentlichen