You are currently viewing Class Object Describes an Equal Method to Compare Objects

Class Object Describes an Equal Method to Compare Objects

Spread the love

equal() Method

The method equals() in java is used to compare if two objects are same or not. equals() method compares the references of two objects; it checks if the references of two objects are pointing to the same reference and therefore returns true or false.


Class Object defines an equal() method to compare objects

equals() is used to compare the current object’s instance with the passed object, so if the two objects carry the same value, the equals() returns a true output. The second object’s output is ‘false’, this is because, even though they are identical, they carry different names, and lastly, the third object also returns false because the class types are different for both ‘Emidiv’ object and an instance class Object.

The second compares to see whether the object being parsed is null or input as a different class, if input as a different class equals(), returns a “false” output.


Thirdly, equals() checks if two objects have the same field value, if they match each other, they do have the equal field value which automatically means that they are the same.


* It’s an awesome way of checking if two objects are equal due to the fact that it has the tool to find out if two objects carry the same contents.

* When comparing class, the equal() method will always provide a “false” output, due to each object being different by virtue of address. It’s almost impossible to extend an instance class and add a value element while maintaining the equals agreement.
* Only provides reference equality that is, variable identity.


Alternative for Equality

Alternative to equality, is to create an instance of a variable for equality, which could be done through inheritance using a method called the accessor method ‘getClass’ through the usage of ‘this’ and ‘super’ in which case instances of the type and subtypes are not allowed to be equal. ‘instanceof’ operator can also be used as an alternative through the usage of ‘if’ checkers.

Override: In logical equality, equals() would only compare object rather, it would only compare the value of the references, the only way to have it do so, is to override the equals()



The equality “==’ operator is often used for comparison of primitive data and objects in java, it does this by comparing basics like int, float, char, short, long, double and boolean however it creates some sorts of misperception when creating objects with equals() in Java, which also is based on memory, which means it would only return true if two objects compared are representing the same object, if not, it would return false.




Class Object defines an equal() method to compare objects

Leave a Reply