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

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

    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.

提交回复
热议问题