HashCode 理解测试

java 代码
  1. public class HashCodeTest {   
  2.   
  3.     /**  
  4.      * @throws java.lang.Exception  
  5.      */  
  6.     @Before  
  7.     public void setUp() throws Exception {}   
  8.   
  9.     /**  
  10.      * @throws java.lang.Exception  
  11.      */  
  12.     @After  
  13.     public void tearDown() throws Exception {}   
  14.        
  15.     @Test  
  16.     public void testHashCode(){   
  17.         String a = new String("AA");   
  18.         String b = a;   
  19.         String c = new String("AA");   
  20.         String d = new String("BB");   
  21.         String e = "AA";   
  22.         String f = "AA";   
  23.            
  24.         System.out.println("String a hashcode is : "+ a.hashCode());   
  25.         System.out.println("String b hashcode is : "+ b.hashCode());   
  26.         System.out.println("String c hashcode is : "+ c.hashCode());   
  27.         System.out.println("String d hashcode is : "+ d.hashCode());   
  28.         System.out.println("String e hashcode is : "+ e.hashCode());   
  29.         System.out.println("String f hashcode is : "+ f.hashCode());   
  30.            
  31.         System.out.println("a.equals(b) : "+ a.equals(b));   
  32.         System.out.println("a==b : "+ (a==b));   
  33.         System.out.println("a.equals(c) : "+ a.equals(c));   
  34.         System.out.println("a==c : "+ (a==c));   
  35.         System.out.println("b.equals(c) : "+ b.equals(c));   
  36.         System.out.println("b==c : "+ (b==c));     
  37.         System.out.println("a.equals(e) : "+ a.equals(e));   
  38.         System.out.println("a==e : "+ (a==e));   
  39.         System.out.println("e.equals(f) : "+ e.equals(f));   
  40.         System.out.println("e==f : "+ (e==f));   
  41.            
  42.            
  43.         Person p1 = new Person();   
  44.         Person p2 = p1;   
  45.         Person p3 = new Person();   
  46.            
  47.         System.out.println("Model p1 hashcode is : "+ p1.hashCode());   
  48.         System.out.println("Model p2 hashcode is : "+ p2.hashCode());   
  49.         System.out.println("Model p3 hashcode is : "+ p3.hashCode());   
  50.         System.out.println("p1.equals(p2) : "+ p1.equals(p2));   
  51.         System.out.println("p1==p2 : "+ (p1==p2));   
  52.         System.out.println("p1.equals(p3) : "+ p1.equals(p3));   
  53.         System.out.println("p1==p3 : "+ (p1==p3));   
  54.         System.out.println("p2.equals(p3) : "+ p2.equals(p3));   
  55.         System.out.println("p2==p3 : "+ (p2==p3));   
  56.     }   
  57.   
  58. }  

打印的结果为:

java 代码
  1. String a hashcode is : 2080  
  2. String b hashcode is : 2080  
  3. String c hashcode is : 2080  
  4. String d hashcode is : 2112  
  5. String e hashcode is : 2080  
  6. String f hashcode is : 2080  
  7. a.equals(b) : true  
  8. a==b : true  
  9. a.equals(c) : true  
  10. a==c : false  
  11. b.equals(c) : true  
  12. b==c : false  
  13. a.equals(e) : true  
  14. a==e : false  
  15. e.equals(f) : true  
  16. e==f : true  
  17. Model p1 hashcode is : 12934710  
  18. Model p2 hashcode is : 12934710  
  19. Model p3 hashcode is : 28064776  
  20. p1.equals(p2) : true  
  21. p1==p2 : true  
  22. p1.equals(p3) : false  
  23. p1==p3 : false  
  24. p2.equals(p3) : false  
  25. p2==p3 : false  

结论:

待续。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值