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

前端 未结 15 1220
礼貌的吻别
礼貌的吻别 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:53

    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
    }
    

提交回复
热议问题