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

2018-12-31 05:06发布

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

16条回答
十年一品温如言
2楼-- · 2018-12-31 05:11

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
}
查看更多
浅入江南
3楼-- · 2018-12-31 05:13
 /** Comparing two or more variables. Here the two items are compared and it will return a boolean true or false.
 * 
 * @return equals
 */
public boolean equals(Object item)
{
    String emp1 = this.toString();
    String emp2 = item.toString();
    return emp1.equals(emp2);
}

@Override
//In case you have more than one variable.
public String toString()
{
    return a + b;
}

// a and b represent the variables to display.
查看更多
谁念西风独自凉
4楼-- · 2018-12-31 05:15

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

查看更多
永恒的永恒
5楼-- · 2018-12-31 05:15

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 + '\'' +
            '}';
}
}
查看更多
弹指情弦暗扣
6楼-- · 2018-12-31 05:16

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
}
查看更多
临风纵饮
7楼-- · 2018-12-31 05:18

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.
        */

        if(this.a.compareTo(someMyClass.a) == 0) return 0; 
        return 1;
    }

    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");
        }
    }
}
查看更多
登录 后发表回答