//对象的identityHashCode()相同,一定是相等的对象
public class IdentityHashCodeDemo {
public static void main(String[] args) {
String str1 = new String("Hello World");
String str2 = new String("Hello World");
System.out.println(str1.equals(str2));//true
System.out.println(str1.hashCode()==str2.hashCode());//true
System.out.println(System.identityHashCode(str1)+"====="+System.identityHashCode(str2));//值不同
String str3="Java学习之法";
String str4="Java学习之法";
System.out.println(System.identityHashCode(str3)+"====="+System.identityHashCode(str4));//值相同
}
}
public class IdentityHashCodeDemo {
public static void main(String[] args) {
String str1 = new String("Hello World");
String str2 = new String("Hello World");
System.out.println(str1.equals(str2));//true
System.out.println(str1.hashCode()==str2.hashCode());//true
System.out.println(System.identityHashCode(str1)+"====="+System.identityHashCode(str2));//值不同
String str3="Java学习之法";
String str4="Java学习之法";
System.out.println(System.identityHashCode(str3)+"====="+System.identityHashCode(str4));//值相同
}
}
本文通过示例代码展示了Java中使用System.identityHashCode方法来获取对象的身份哈希码,并对比了不同情况下对象的equals方法与身份哈希码之间的区别。文章强调了即使两个对象的内容相同,它们的身份哈希码也可能不同。
743

被折叠的 条评论
为什么被折叠?



