Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two objects with .equals() and == operator

I constructed a class with one String field. Then I created two objects and I have to compare them using == operator and .equals() too. Here's what I've done:

public class MyClass {      String a;      public MyClass(String ab) {         a = ab;     }      public boolean equals(Object object2) {         if(a == object2) {              return true;         }         else return false;     }      public boolean equals2(Object object2) {         if(a.equals(object2)) {             return true;         }         else return false;     }        public static void main(String[] args) {          MyClass object1 = new MyClass("test");         MyClass object2 = new MyClass("test");          object1.equals(object2);         System.out.println(object1.equals(object2));          object1.equals2(object2);         System.out.println(object1.equals2(object2));     }   } 

After compile it shows two times false as a result. Why is it false if the two objects have the same fields - "test"?

like image 406
Fastkowy Avatar asked Nov 14 '12 21:11

Fastkowy


People also ask

What is the difference between equals () and == operator?

equals() method. The major difference between the == operator and . equals() method is that one is an operator, and the other is the method. Both these == operators and equals() are used to compare objects to mark equality.

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

equals() method in Java. Both equals() method and the == operator are used to compare two objects in Java. == is an operator and equals() is method. But == operator compares reference or memory location of objects in a heap, whether they point to the same location or not.

Can you use == to compare objects?

The == operator compares whether two object references point to the same object.

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

== should be used during reference comparison. == checks if both references points to same location or not. equals() method should be used for content comparison. equals() method evaluates the content to check the equality.


1 Answers

== compares object references, it checks to see if the two operands point to the same object (not equivalent objects, the same object).

If you want to compare strings (to see if they contain the same characters), you need to compare the strings using equals.

In your case, if two instances of MyClass really are considered equal if the strings match, then:

public boolean equals(Object object2) {     return object2 instanceof MyClass && a.equals(((MyClass)object2).a); } 

...but usually if you are defining a class, there's more to equivalency than the equivalency of a single field (a in this case).


Side note: If you override equals, you almost always need to override hashCode. As it says in the equals JavaDoc:

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

like image 175
T.J. Crowder Avatar answered Oct 05 '22 13:10

T.J. Crowder