Java源码解析:hashCode与相同对象的关系
生活随笔
收集整理的這篇文章主要介紹了
Java源码解析:hashCode与相同对象的关系
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.普通類對象
1. hashCode相同,不一定是同一個對象
2. 同一個對象的,hashCode值一定相同
2. 數值型的原始數據類型對應的包裝類
只要值是一樣的,hashCode就會是相同的。盡管不同的數值類型的包裝類,計算hashCode的算法不一樣,但是底層都是拿對應的原始數據類型的值去進行hashCode計算。
以Double類為例
3. 測試代碼如下
/***hashCode相同,不一定是同一個對象*同一個對象的,hashCode值一定相同 **-------------------------------------------------------------------------------*普通對象的HashCode值源碼解釋:*If two objects are equal according to the equals(Object) method, then calling *the hashCode method on each of the two objects must produce the same integer result. *-------------------------------------------------------------------------------*It is not required that if two objects are unequal according to the *java.lang.Object.equals(java.lang.Object) method, then calling the hashCode *method on each of the two objects must produce distinct integer results.*-------------------------------------------------------------------------------*However, the programmer should be aware that producing distinct integer results *for unequal objects may improve the performance of hash tables. *-------------------------------------------------------------------------------**/ public class Test {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public Test() {super();}public Test(String name) {super();this.name = name;}public static void main(String[] args) {System.out.println("--------------------普通對象-----------------------");Test test3=new Test();Test test4=new Test();System.out.println(test3.hashCode());//2018699554System.out.println(test4.hashCode());//1311053135Test test=new Test("我");Test test2=new Test("我");System.out.println(test.hashCode());//366712642System.out.println(test2.hashCode());//1829164700System.out.println("--------------------String-----------------------");String s1="abc";String s2="abc";System.out.println(s1.hashCode());//96354System.out.println(s2.hashCode());//96354String s5=new String("abc");String s6=new String("abc");System.out.println(s5.hashCode());//96354System.out.println(s6.hashCode());//96354String s3=new String();String s4=new String();System.out.println(s3.hashCode());//0System.out.println(s4.hashCode());//0/*** 數值型原始類型對應的包裝類(Byte,Short,Integer,Float,Double),hashCode算法都是基于* 對應的原始數據類型,所以只要包裝類的數值相同,那么hashCode必然相同* * Double類關于hashCode源碼說明:* Double類的hashCode是根據對應的double值計算獲得的。* * Returns a hash code for a {@code double} value; compatible with* {@code Double.hashCode()}.** @param value the value to hash* @return a hash code value for a {@code double} value.* @since 1.8*//*Double類關于hashCode源碼:* public static int hashCode(double value) {long bits = doubleToLongBits(value);return (int)(bits ^ (bits >>> 32));}*/System.out.println("--------------------原始類型對應的包裝類-----------------------");Double d1=5.0;Double d2=5.0;System.out.println(d1.hashCode());//1075052544System.out.println(d2.hashCode());//1075052544Double d3=new Double(5.0);Double d4=new Double(5.0);System.out.println(d3.hashCode());//1075052544System.out.println(d4.hashCode());//1075052544} }總結
以上是生活随笔為你收集整理的Java源码解析:hashCode与相同对象的关系的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Integer类对象池与==问题:Int
- 下一篇: Java源码解析:深入理解==和equa