Situatie
Java Object class is the super class of all the Java classes. All Java classes implements the Object class by default. The Java Object class provides the two important methods to compare two obiects in java, i.e. equals() and hashCode() method. In this section, we will learn how equals() and hashCode() method works. Along with this, we will also learn how to compare two objects in Java with proper examples.
Java provides the two methods of the Object class to compare the objects are as follows:
- Java equals() Method
- Java hashCode() Method
Java equals() Method
The equals() method of the Object class compare the equality of two objects. The two objects will be equal if they share the same memory address.
Syntax:
- public boolean equals(Object obj)
The method parses a reference object as a parameter. It returns true if the objects are equal, else returns false.
It is also possible that an object is equal to another given object, then the equals() method follow the equivalence relation to compare the objects.
-
- Reflexive: If x is a non-null reference, the calling of x.equals(x) must return true.
- Symmetric: If the two non-null references are x and y, x.equals(y) will return true if and only if y.equals(x) return true.
- Transitive: If the three non-null references are x, y, and z, x.equals(z) will also return true if x.equals(y) and y.equals(z) both returns true.
- Consistent: If the two non-null references are x and y, the multiple calling of x.equals(y) constantly returns either true or false. It does not provide any information used in the comparison.
- For any non-null reference x, x.equals(null) returns false.
In short, for any non-null reference say x and y, it returns true if and only if both references refer to the same object.Remember: When we override the equals() method, it is necessary to override the hashCode() method. Overriding follow the convention for the hashCode() method that states, the equal object must have equal hash code.
Example of equals() method
In the following example, we have created constructor of the Double and Long class and passes corresponding values, as an argument that stored in their objects, respectively.
After that, in the first println statement, we have invoked equals() method and parse an object y as a parameter that compares the object x and y. It returns false because x holds the double value and y holds the long value that is not equal.
Similarly, in the second println statement, we have invoked equals() method and parse the same value as in the constructor of the Double class. It returns true because the object of double class i.e. x holds the same value as we have passed in the equals() method.
Backup
- public class ObjectComparisonExample
- {
- public static void main(String[] args)
- {
- //creating constructor of the Double class
- Double x = new Double(123.45555);
- //creating constructor of the Long class
- Long y = new Long(9887544);
- //invoking the equals() method
- System.out.println(“Objects are not equal, hence it returns “ + x.equals(y));
- System.out.println(“Objects are equal, hence it returns “ + x.equals(123.45555));
- }
- }
Output:
Objects are not equal, hence it returns false Objects are equal, hence it returns true
Leave A Comment?