下面是例子:
- package com.barneyx.args;
- /**
- * Created by Administrator on 2017/2/11.
- */
- public class IdentityHashCodeTest {
- public static void main(String[] args) {
- //这里是两个对象,两个不同的地址,所以肯定HashCode的值是不同的
- String s1 = new String("hello");
- String s2 = new String("hello");
- System.out.println(
- s1.hashCode()+
- "---------------"+
- s2.hashCode()
- );
-
- System.out.println(System.identityHashCode(s1)+"-----"+System.identityHashCode(s2));
- String s3 = "Java";
- String s4 = "Java";
-
- //s3和s4是相同的字符串所以他们只有一分引用,所以他们的HashCode是相同的
- System.out.println(System.identityHashCode(s3)+"-----------"+System.identityHashCode(s4));
- }
- }
- /******************************
- 运行结果:
- 99162322---------------99162322
- 460141958-----1163157884
- 1956725890-----------1956725890
- Process finished with exit code 0
- *******************************/