Are two Java objects with same hashcodes not necessarily equal?

后端 未结 9 2172
礼貌的吻别
礼貌的吻别 2020-11-27 06:34

I understand why providing same hashcode for two equal (through equals) objects is important. But is the vice versa true as well, if two objects have same hash

9条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 07:24

    hashCode value depends on the implementation. for example String class implements hashCode() function depending upon the value. it means

    String a=new String("b");
    String b=new String("b");
    

    will have same hashcode but, these are two different objects. and a==b will return false.

提交回复
热议问题