What is the Relation between equals() and (==) Operator?

What is the Relation and Difference between .equals() and == Operator?

What is the Relation between equals() and (==) Operator?

What is the Relation between equals() and (==) Operator?

(==) operator is a binary operator in java which compares the two objects based on their location in the memory. That means if two reference variables are pointing to the same object in the memory then applying (==) operator on those reference variables will return true. You can apply (==) operator to both primitive types as well as derived types. But, it is best suitable for primitive types.

  • (==) is a reference comparison/address comparison, i.e. both objects point to the same memory location

equals() method is a public member of java.lang.Object class. As all classes in Java extend Object class by default, this method is available in all classes you create in Java. The default version of equals() method does the same thing as (==) operator i.e comparing the two objects based on their location in the memory. But, it is always recommended to override the equals() method so that it performs the comparison of two objects based on their content or based on any business logic. That means, if two objects satisfy the business logic, then calling equals method on those objects should return true, irrespective of their location in the memory.

  • .equals() evaluates the comparison of values in the objects

Relation between .equals() and == Operator

  • If two object is equal by == Operator then these objects are always equal by .equals(). That is if obj1==obj2 is true then obj1.equals(obj2) is always true.
  • If two objects are not equal by == Operator then we can’t conclude anything about .equals() method. It may return true or false.
    That is if obj1==obj2 is false then obj1.equals(obj2) may return true or false and we can’t expect exactly.
  • If two objects are equals by .equals() method then we can’t conclude anything about == Operator. It may return or false. That is
    obj1.equals(obj2) is true then we can’t conclude about obj1==obj2. it may return true or false.
  • If two objects are not equal by .equals() method then these objects are always not by == Operator. That is obj1.equals(obj2) is false then obj1==obj2 is always false.

Difference between == Operator and .equals()

  • To use == Operator compulsory there should be some relation between arguments types(either child to parent or parent to child or same type) otherwise we will get compile type error saying Incomparable Types.
  • If there is no relation between arguments types then .equals() won’t rise any compile time or runtime errors simply it returns false.
== Operator .equals() method
1 It is an operator in Java applicable for both primitives and object types. It is a method applicable only for object types but not primitives.
2 In the case of object references == Operator meant for reference comparison(Address comparison). By default .equals method present in object class also meant for reference comparison.
3 We can override == Operator for content comparison. We can override .equals() method for content comparison.
4 To use == Operator compulsory there should some relation between arguments types(either child to parent or child to the parent or same type) otherwise we will get compile time saying incomparable types. If there is no relation between arguments types then .equals method won’t rise any compile time or runtime error and simply return false.

Conclusion:- In journal we can use == Operator for reference comparison and .equals method for content comparison.

Leave a Reply

Your email address will not be published. Required fields are marked *

*