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:21

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());
   }
查看更多
梦该遗忘
3楼-- · 2018-12-31 05:25

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 .

查看更多
看淡一切
4楼-- · 2018-12-31 05:27

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.

查看更多
余欢
5楼-- · 2018-12-31 05:32

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

查看更多
无色无味的生活
6楼-- · 2018-12-31 05:35

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

查看更多
不再属于我。
7楼-- · 2018-12-31 05:35

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

查看更多
登录 后发表回答