java 中两个 String 具有相同的 hashCode 使用 == 判断返回 false ?
本文介绍了java 中两个 String 具有相同的 hashCode 使用 == 判断返回 false ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
String str1 = new String("hello");
String str2 = new String("hello");
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));
System.out.println(str1.hashCode());
System.out.println(str2.hashCode());
代码如上,输出结果:
false
true
99162322
99162322
为什么具有相同的 hashCode
返回的是 false
呢
解决方案
看书的时候不仔细,弄清楚了;String
重写了 hashCode
方法,其 hashCode
值是由内容决定的,而不是地址决定的
这篇关于java 中两个 String 具有相同的 hashCode 使用 == 判断返回 false ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文