What is the difference between == and equals() in Java? What is the difference between == and equals() in Java? java java

What is the difference between == and equals() in Java?


In general, the answer to your question is "yes", but...

  • .equals(...) will only compare what it is written to compare, no more, no less.
  • If a class does not override the equals method, then it defaults to the equals(Object o) method of the closest parent class that has overridden this method.
  • If no parent classes have provided an override, then it defaults to the method from the ultimate parent class, Object, and so you're left with the Object#equals(Object o) method. Per the Object API this is the same as ==; that is, it returns true if and only if both variables refer to the same object, if their references are one and the same. Thus you will be testing for object equality and not functional equality.
  • Always remember to override hashCode if you override equals so as not to "break the contract". As per the API, the result returned from the hashCode() method for two objects must be the same if their equals methods show that they are equivalent. The converse is not necessarily true.


With respect to the String class:

The equals() method compares the "value" inside String instances (on the heap) irrespective if the two object references refer to the same String instance or not. If any two object references of type String refer to the same String instance then great! If the two object references refer to two different String instances .. it doesn't make a difference. Its the "value" (that is: the contents of the character array) inside each String instance that is being compared.

On the other hand, the "==" operator compares the value of two object references to see whether they refer to the same String instance. If the value of both object references "refer to" the same String instance then the result of the boolean expression would be "true"..duh. If, on the other hand, the value of both object references "refer to" different String instances (even though both String instances have identical "values", that is, the contents of the character arrays of each String instance are the same) the result of the boolean expression would be "false".

As with any explanation, let it sink in.

I hope this clears things up a bit.


There are some small differences depending whether you are talking about "primitives" or "Object Types"; the same can be said if you are talking about "static" or "non-static" members; you can also mix all the above...

Here is an example (you can run it):

public final class MyEqualityTest{    public static void main( String args[] )    {        String s1 = new String( "Test" );        String s2 = new String( "Test" );        System.out.println( "\n1 - PRIMITIVES ");        System.out.println( s1 == s2 ); // false        System.out.println( s1.equals( s2 )); // true        A a1 = new A();        A a2 = new A();        System.out.println( "\n2 - OBJECT TYPES / STATIC VARIABLE" );        System.out.println( a1 == a2 ); // false        System.out.println( a1.s == a2.s ); // true        System.out.println( a1.s.equals( a2.s ) ); // true        B b1 = new B();        B b2 = new B();        System.out.println( "\n3 - OBJECT TYPES / NON-STATIC VARIABLE" );        System.out.println( b1 == b2 ); // false        System.out.println( b1.getS() == b2.getS() ); // false        System.out.println( b1.getS().equals( b2.getS() ) ); // true    }}final class A{    // static    public static String s;    A()    {        this.s = new String( "aTest" );    }}final class B{    private String s;    B()    {        this.s = new String( "aTest" );    }    public String getS()    {        return s;    }}

You can compare the explanations for "==" (Equality Operator) and ".equals(...)" (method in the java.lang.Object class) through these links: