hashcode值,内存地址,判断相等

2018-11-30 20:58:14
  • equals 比较(重写后的) 内部重写“哈希码”方法 对象相等hashcode必须相等
  • ==判断 比较(未重写的) 未重写“哈希码”方法 System.identityHashCode(x) 这个hashcode是和内存地址挂钩的 如果hashcode不等则结果不等

测试用例:

  1. package cn.wangsr.testHashcode;
  2. public class test01 {
  3. public static void main(String[] args) {
  4. //******************************
  5. System.out.println("============users对象=============");
  6. users a=new users();
  7. a.setId(1111);
  8. a.setUsername("asv");
  9. a.setDescription("bnv");
  10. users b=new users();
  11. b.setId(1111);
  12. b.setUsername("asv");
  13. b.setDescription("bnv");
  14. System.out.println("=======重写hashcode============");
  15. System.out.println(a.hashCode());
  16. System.out.println(b.hashCode());
  17. System.out.println(a.equals(b)); //当user中未重写hashcode时 结果为false
  18. System.out.println("=======identityHashCode============");
  19. System.out.println(System.identityHashCode(a));
  20. System.out.println(System.identityHashCode(b));
  21. System.out.println(a==b);
  22. //********************************
  23. System.out.println("============包装类对象=============");
  24. Integer cInteger=new Integer(99);
  25. Integer dInteger=new Integer(99);
  26. System.out.println("=======重写hashcode============");
  27. System.out.println(cInteger.hashCode());
  28. System.out.println(dInteger.hashCode());
  29. System.out.println("=======identityHashCode============");
  30. System.out.println(System.identityHashCode(cInteger));
  31. System.out.println(System.identityHashCode(dInteger));
  32. System.out.println(cInteger==dInteger);
  33. //************************************
  34. String eString=new String("ww");
  35. String fString=new String("ww");
  36. System.out.println("=======重写hashcode============");
  37. System.out.println(eString.hashCode());
  38. System.out.println(fString.hashCode());
  39. System.out.println("=======identityHashCode============");
  40. System.out.println(System.identityHashCode(eString));
  41. System.out.println(System.identityHashCode(fString));
  42. System.out.println(eString==fString);
  43. System.out.println("====================");
  44. //***************************
  45. /**
  46. * equals 比较 内部重写“哈希码”方法 对象相等hashcode必须相等
  47. *
  48. * ==判断 比较 未重写“哈希码”====》 System.identityHashCode(x) 这个hashcode是和内存地址挂钩的 hashcode不等结果不等
  49. *
  50. */
  51. /**
  52. * 常量hashcode ======常量储存区
  53. */
  54. System.out.println("=======常量============");
  55. String gString="dd";
  56. String hString="dd";
  57. System.out.println("=======重写hashcode============");
  58. System.out.println(gString.hashCode());
  59. System.out.println(hString.hashCode());
  60. System.out.println("=======identityHashCode============");
  61. System.out.println(System.identityHashCode(gString));
  62. System.out.println(System.identityHashCode(hString));
  63. System.out.println(gString==hString);
  64. }
  65. }

结果:

  1. ============users对象=============
  2. =======重写hashcode============
  3. 94056598
  4. 94056598
  5. true
  6. =======identityHashCode============
  7. 17225372
  8. 5433634
  9. false
  10. ============包装类对象=============
  11. =======重写hashcode============
  12. 99
  13. 99
  14. =======identityHashCode============
  15. 2430287
  16. 17689166
  17. false
  18. =======重写hashcode============
  19. 3808
  20. 3808
  21. =======identityHashCode============
  22. 6585861
  23. 26253138
  24. false
  25. ====================
  26. =======常量============
  27. =======重写hashcode============
  28. 3200
  29. 3200
  30. =======identityHashCode============
  31. 33121026
  32. 33121026
  33. true

0
2
0

添加评论

正在回复:
取消
3
0
2
0