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

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

    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

提交回复
热议问题