Compare two objects with .equals() and == operator
Asked Answered
W

17

91

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"?

Woodsia answered 14/11, 2012 at 21:35 Comment(4)
Btw, looking at equals and equals2: any time you have something of the form if(a) { return true; } else { return false; } you should probably just write return a.Yt
@Yt You mean, with change from boolean to String?Woodsia
no, your code is asking if a boolean is a true, and returning true if it is and false otherwise. So for instance, if(a.equals(object2)) { return true; } else return false could just be return a.equals(object2).Yt
possible duplicate of How do I compare strings in Java?Fifteen
H
156

== 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.

Hartford answered 14/11, 2012 at 21:38 Comment(2)
@TJ In == compares object references, to elaborate, Does it mean == compares hashcode of the two objects?Accounting
@NarendraJaggi - No, it means that the JVM checks to see if the two operands both refer to the same object. How it does that is up to the JVM, but there's no reason to think it would use a hashcode to do it.Hartford
D
22

You should override equals

 public boolean equals (Object obj) {
     if (this==obj) return true;
     if (this == null) return false;
     if (this.getClass() != obj.getClass()) return false;
     // Class name is Employ & have lastname
     Employe emp = (Employee) obj ;
     return this.lastname.equals(emp.getlastname());
 }
Demure answered 15/7, 2015 at 11:38 Comment(5)
This is arguably the best answer, however you might want to use this.equals(obj) instead of (this == null) for non primitive typesDeach
I'd argue that the if (this == null) case is unnecessary anyway; calling nullObject.equals(whatever) is going to throw a null pointer exception, so we can safely assume that this is not null in any Java method we may write.Blackout
I'd argue that if (this == null) is not unnecessary, but wrong object checked. Before the third line we need obj checked so the second if should be if (obj == null)Brainard
@Deach That would lead to a StackOverflowException, as it would recursively call the same equals method without any stop condition.Callean
this == null will always be false, since this is a reference to the current object — which can never be null!Callean
C
7

The best way to compare 2 objects is by converting them into json strings and compare the strings, its the easiest solution when dealing with complicated nested objects, fields and/or objects that contain arrays.

sample:

import com.google.gson.Gson;


Object a = // ...;
Object b = //...;
String objectString1 = new Gson().toJson(a);
String objectString2 = new Gson().toJson(b); 

if(objectString1.equals(objectString2)){
    //do this
}
Circuit answered 22/2, 2016 at 20:25 Comment(3)
I would like to call this: overkill.Millwater
@Rolfツ Why this is overkill in your opinion? I've looked for a solution to this problem and this is the easiest solution I've found so far. Any better suggestions are welcome.Olag
Because with Java you can compare objects without first creating a Gson object and then calling toJson. Creating the Gson object and calling the logic needed to convert the actually object to a flat String (toJson) is unnecessary overhead. You can compare objects without first converting the objects to Json strings (which is also quicker).Millwater
M
6

It looks like equals2 is just calling equals, so it will give the same results.

Microtome answered 14/11, 2012 at 21:38 Comment(3)
OP is calling the equals method of the String a which is a member of the class. equals2 is not calling equalsBar
Yes, thanks. Looks like I totally missed the confusion between MyClass and String which is the real problem.Microtome
to be more specific equals2() compares a which is String with object2 which is Object and probably MyClass which would in normal usage always return false.Brainard
E
6

The overwrite function equals() is wrong. The object "a" is an instance of the String class and "object2" is an instance of the MyClass class. They are different classes, so the answer is "false".

Enchanter answered 1/7, 2015 at 13:0 Comment(0)
A
4

Your equals2() method always will return the same as equals() !!

Your code with my comments:

public boolean equals2(Object object2) {  // equals2 method
    if(a.equals(object2)) { // if equals() method returns true
        return true; // return true
    }
    else return false; // if equals() method returns false, also return false
}
Alcinia answered 14/11, 2012 at 21:38 Comment(1)
Just return a.equals(object2);Relict
S
3

The "==" operator returns true only if the two references pointing to the same object in memory. The equals() method on the other hand returns true based on the contents of the object.

Example:

String personalLoan = new String("cheap personal loans");
String homeLoan = new String("cheap personal loans");

//since two strings are different object result should be false
boolean result = personalLoan == homeLoan;
System.out.println("Comparing two strings with == operator: " + result);

//since strings contains same content , equals() should return true
result = personalLoan.equals(homeLoan);
System.out.println("Comparing two Strings with same content using equals method: " + result);

homeLoan = personalLoan;
//since both homeLoan and personalLoan reference variable are pointing to same object
//"==" should return true
result = (personalLoan == homeLoan);
System.out.println("Comparing two reference pointing to same String with == operator: " + result);

Output: Comparing two strings with == operator: false Comparing two Strings with same content using equals method: true Comparing two references pointing to same String with == operator: true

You can also get more details from the link: http://javarevisited.blogspot.in/2012/12/difference-between-equals-method-and-equality-operator-java.html?m=1

Sunward answered 19/10, 2016 at 18:23 Comment(0)
C
2

Statements a == object2 and a.equals(object2) both will always return false because a is a string while object2 is an instance of MyClass

Constituency answered 1/8, 2014 at 9:1 Comment(0)
H
2

Your implementation must like:

public boolean equals2(Object object2) {
    return a.equals(object2.a);
}

With this implementation your both methods would work.

Hayton answered 31/10, 2014 at 15:52 Comment(0)
F
2

Your class might implement the Comparable interface to achieve the same functionality. Your class should implement the compareTo() method declared in the interface.

public class MyClass implements Comparable<MyClass>{

    String a;

    public MyClass(String ab){
        a = ab;
    }

    // returns an int not a boolean
    public int compareTo(MyClass someMyClass){ 

        /* The String class implements a compareTo method, returning a 0 
           if the two strings are identical, instead of a boolean.
           Since 'a' is a string, it has the compareTo method which we call
           in MyClass's compareTo method.
        */

        return this.a.compareTo(someMyClass.a);

    }

    public static void main(String[] args){

        MyClass object1 = new MyClass("test");
        MyClass object2 = new MyClass("test");

        if(object1.compareTo(object2) == 0){
            System.out.println("true");
        }
        else{
            System.out.println("false");
        }
    }
}
Flummox answered 9/10, 2016 at 19:9 Comment(0)
F
2

If you dont need to customize the default toString() function, another way is to override toString() method, which returns all attributes to be compared. then compare toString() output of two objects. I generated toString() method using IntelliJ IDEA IDE, which includes class name in the string.

public class Greeting {
private String greeting;

@Override
public boolean equals(Object obj) {
    if (this == obj) return true;
    return this.toString().equals(obj.toString());
}

@Override
public String toString() {
    return "Greeting{" +
            "greeting='" + greeting + '\'' +
            '}';
}
}
Fortification answered 1/5, 2018 at 7:16 Comment(0)
A
1

the return type of object.equals is already boolean. there's no need to wrap it in a method with branches. so if you want to compare 2 objects simply compare them:

boolean b = objectA.equals(objectB);

b is already either true or false.

Aranda answered 6/8, 2014 at 20:36 Comment(0)
M
1

When we use == , the Reference of object is compared not the actual objects. We need to override equals method to compare Java Objects.

Some additional information C++ has operator over loading & Java does not provide operator over loading. Also other possibilities in java are implement Compare Interface .which defines a compareTo method.

Comparator interface is also used compare two objects

Mcmillin answered 8/9, 2014 at 3:59 Comment(1)
Consider that your answer adds nothing that wasn't said almost 2 years ago.Deductive
G
1

Here the output will be false , false beacuse in first sopln statement you are trying to compare a string type varible of Myclass type to the other MyClass type and it will allow because of both are Object type and you have used "==" oprerator which will check the reference variable value holding the actual memory not the actual contnets inside the memory . In the second sopln also it is the same as you are again calling a.equals(object2) where a is a varible inside object1 . Do let me know your findings on this .

Glyco answered 6/7, 2017 at 14:26 Comment(1)
Welcome to stackoverflow bidyadhar. The question is dated 14/11/2012 and got already a good response (approved by OP). The one that you got is correct, but it is not adding useful info. I suggest you to read the rules before doing anythingHamitic
H
0

In short, == compares two POINTERS.

If the two pointers are equal, then they both point to same object in memory (which, obviously has the same value as itself).

However, .equals will compare the VALUES of whatever is pointed to, returning true iff they both evaluate to the same value.
Thus, two separate strings (i.e., at different addresses in memory) are always != but are .equal iff they contain the same (null-terminated) sequence of chars.

Hic answered 23/7, 2022 at 14:43 Comment(0)
L
0

You should override "equals" method and use "EqualsBuilder" and "HashCodeBuilder" from this Java Utility Apache Commons Lang

Lamellirostral answered 7/2, 2024 at 22:48 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Corrade
A
-3

In the below code you are calling the overridden method .equals()

public boolean equals2(Object object2) {
    if(a.equals(object2)) { // here you are calling the overriden method, that is why you getting false 2 times.
        return true;
    }
    else return false;
}
Aldehyde answered 14/5, 2014 at 15:17 Comment(1)
No, a.equals is string's method, it's not overriden anywhere.Genvieve

© 2022 - 2025 — McMap. All rights reserved.