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

前端 未结 15 1573
礼貌的吻别
礼貌的吻别 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: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());
     }
    

提交回复
热议问题