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

前端 未结 15 1222
礼貌的吻别
礼貌的吻别 2020-11-22 01:13

I constructed a class with one String field. Then I created two objects and I have to compare them using == operator and .equals() too

相关标签:
15条回答
  • 2020-11-22 01:27

    IN the below code you are calling the overriden 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; }

    0 讨论(0)
  • 2020-11-22 01:28

    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 .

    0 讨论(0)
  • 2020-11-22 01:30

    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());
     }
    
    0 讨论(0)
  • 2020-11-22 01:32

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

    0 讨论(0)
  • 2020-11-22 01:32

    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 + '\'' +
                '}';
    }
    }
    
    0 讨论(0)
  • 2020-11-22 01:36

    Your implementation must like:

    public boolean equals2(Object object2) {
        if(a.equals(object2.a)) {
            return true;
        }
        else return false;
    }
    

    With this implementation your both methods would work.

    0 讨论(0)
提交回复
热议问题