Equality operator ‘==’ and ‘equals’ method in JAVA

I come across queries or participate in debates relating to operator ‘==’ and ‘equals’ method in JAVA frequently. Following is a concise explanation of its definition and usage in Java.

The equality Operator ‘==’

It can be used to compare

1. two operands of numeric type,
2. two operands of type boolean, 
3. two operands that are each of either reference type or the null type.To decipher the above definition: To compare the numeric and boolean types are trivial to understand. The third point is, the equality operator checks if the two operands refers to the same object. That is, if both the operands points to the same reference then the result is ‘true’ otherwise ‘false’. Note that, there is no mention about the content or value of the operand.

Method ‘equals’

There is a method defined in class ‘Object’, which has the signature as “public boolean equals(Object obj)”. This is defined as, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true). Here also there is no mention about the content or value of the objects.

Is it wise to compare ‘==’ and ‘equals’?

‘equals’ is not a ‘final’ method therefore there is a possibility to override it when creating classes. When overriding it, generally it is defined to compare the content/value of the objects and not to compare the reference. One example is the notorious ‘String’ class.

When you talk about the ‘equals’ method, you are speaking about a method that belongs to a single class. So the definition for ‘equals’ cannot be generalized. If that class doesn’t overrides the ‘equals’ method, then the common definition as it is defined in the ‘Object’ class can be taken, since it is inherited.

StringBuffer and its ‘equals’ method

Consider the code segment,

 StringBuffer sb1 = new StringBuffer(”Orange”);
 StringBuffer sb2 = new StringBuffer(”Orange”);
 
Though the values in both the objects are same sb1.equals(sb2) returns ‘false’.Because, ‘equals’ method is not overridden in StringBuffer and it inherits the definition from the Object class. To recollect the defintion, it checks for the object reference and not the content. In this scenario sb1 and sb2 are two different objects pointing to different reference so it returns ‘false’.

String’s ‘equals’ method and the general ‘==’

As mentioned earlier, the String class overrides the ‘equals’ method and creates its own definition. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object. That is, if the content/value is same then it returns ‘true’.

To answer the popular question, when comparing two Strings which should i use ‘==’ or ‘equals’?
It depends on what you want to compare. If you want to compare the values then use ‘equals’, or if you want to compare the reference then use ‘==’.

To the inquisite,
 
Consider the following code segment
  String s1 = “Apple”;
  String s2 = “Apple”;

how come ‘s1==s2‘ returns true. As per the generalized definition for ‘==’, it checks for the equality of reference for the two operands and not the content. This is because of the special nature of the String class.

Only String class in java allows to create a new object in this (String s1 = “Apple”;) special manner that is ‘literal assignment’. JVM keeps a pool of String literals in memory. To reduce object creation and garbage collection overheads, when you create a String object the object pool is searched. If the same string already exists, then new object is not created, instead the reference for the new object is assigned to the already existing object.

So the reference for s2 is assigned to s1 while creating it and so ’s1==s2′ returns ‘true’.

But if,
  String s1 = “Apple”;
  String s2 = new String(”Apple”);

then s1==s2 returns ‘false’. Since s2 is entirely a new object and its reference is different from s1.

s1.equals(s2) will return ‘true’ in both the above cases, because it has nothing to do with the references and it compares the content as per the overridden definition of the String class.

One Response to “Equality operator ‘==’ and ‘equals’ method in JAVA”

  1. Balakrishnan Says:

    This article is very nice. The information about the equals method in StringBuffer class is usefull.

    I think there is a strong relation between equals method and its counterpart hashcode method. So it would be better if you talk about hashcode contract over equals method here.

    Thanks

Leave a Reply