Student s1 = new Student(18,"Kang");
Student s2 = new Student();
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
s2=s1;
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
s2.setAge(22);
System.out.println(s1);
System.out.println(s2);
结果:
可以看出再new 了s2之后,预分配了一块地址,但是系统发现调用引用,于是s3的地址变成了s1引用的地址。