Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the equals() method with String and Object in Java

Object o1 = new Object();
Object o2 = new Object();
//o1=o2;
System.out.println(o1.equals(o2));

It returns false. It can return true, if the comment is removed.


Why isn't the same thing applicable to the String class?

String s1=new String();
String s2=new String();
System.out.println(s1.equals(s2));

It returns true. Why? (because String uses interns or something else involved?)

like image 642
Tiny Avatar asked Oct 21 '12 20:10

Tiny


People also ask

Can you use equals () with objects?

The equals() method is a static method of the Objects class that accepts two objects and checks if the objects are equal. If both the objects point to null , then equals() returns true .

Can we use equals method for String in Java?

Java String equals() MethodThe equals() method compares two strings, and returns true if the strings are equal, and false if not. Tip: Use the compareTo() method to compare two strings lexicographically.

How do you use equals method in Java for objects?

In the first comparison, equals() compares the current object instance with the object that has been passed. If the two objects have the same values, equals() will return true . In the second comparison, equals() checks to see whether the passed object is null, or if it's typed as a different class.

What is the difference between equals () of String class and equals () of Object class?

The equals() method of the String class is not same as the equals() method of the Object class. It is overridden, this method accepts a String value and compares it with the current object and returns true only if the character sequences in the both String objects are exactly same.


2 Answers

Because equals() for String compares the content, not the object itself.

public boolean equals(Object anObject)

Compares this string to the specified object. 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.

    /* String.equals() */
public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = offset;
            int j = anotherString.offset;
            while (n-- != 0) {
                if (v1[i++] != v2[j++])
                    return false;
            }
            return true;
        }
    }
    return false;
}

(Link to the source of String.equals())

Versus the equals for Object:

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, 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).

/* Object.equals() */
public boolean equals(Object obj) {
    return (this == obj);
}

(Link to the source of Object.equals())

Also, don't forget the contract of the equals() function:

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

Also recommended reading:

  • Object.hashCode()
  • Effective Java (Bloch)
like image 168
ppeterka Avatar answered Sep 28 '22 12:09

ppeterka


equals for Object compares memory references.
That is why it is false since they are different Objects
equals for String is overridden to compare based on characters.
You have 2 empty String objects that is why equals returns true.

like image 31
Cratylus Avatar answered Sep 28 '22 11:09

Cratylus