
参考:https://www.cnblogs.com/iliuyuet/p/5603618.html
测试代码:
public class stack {
public static void main(String args[]) {
int a = 3;
int b = 3;
//看不到真正的内存地址的,只能看到虚拟机映射的地址,HASHCODE
int aHashCode = System.identityHashCode(a);
int bHashCode = System.identityHashCode(b);
System.out.println(aHashCode + "\t" + bHashCode);
a = 4;
aHashCode = System.identityHashCode(a);
System.out.println(aHashCode + "\t" + bHashCode);
String str1 = "abc";
String str2 = "abc";
int str1HashCode = System.identityHashCode(str1);
int str2HashCode = System.identityHashCode(str2);
System.out.println(str1HashCode + "\t" + str2HashCode);
// == 比较对象是否相等, equals()比较类里的数值是否相等
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));
String str3 = new String("abc");
String str4 = new String("abc");
int str3HashCode = System.identityHashCode(str3);
int str4HashCode = System.identityHashCode(str4);
System.out.println(str3HashCode + "\t" + str4HashCode);
}
}
网友评论