Java源码解析:hashCode与相同对象的关系
生活随笔
收集整理的這篇文章主要介紹了
Java源码解析:hashCode与相同对象的关系
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.普通類(lèi)對(duì)象
1. hashCode相同,不一定是同一個(gè)對(duì)象
2. 同一個(gè)對(duì)象的,hashCode值一定相同
2. 數(shù)值型的原始數(shù)據(jù)類(lèi)型對(duì)應(yīng)的包裝類(lèi)
只要值是一樣的,hashCode就會(huì)是相同的。盡管不同的數(shù)值類(lèi)型的包裝類(lèi),計(jì)算hashCode的算法不一樣,但是底層都是拿對(duì)應(yīng)的原始數(shù)據(jù)類(lèi)型的值去進(jìn)行hashCode計(jì)算。
以Double類(lèi)為例
3. 測(cè)試代碼如下
/***hashCode相同,不一定是同一個(gè)對(duì)象*同一個(gè)對(duì)象的,hashCode值一定相同 **-------------------------------------------------------------------------------*普通對(duì)象的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("--------------------普通對(duì)象-----------------------");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/*** 數(shù)值型原始類(lèi)型對(duì)應(yīng)的包裝類(lèi)(Byte,Short,Integer,Float,Double),hashCode算法都是基于* 對(duì)應(yīng)的原始數(shù)據(jù)類(lèi)型,所以只要包裝類(lèi)的數(shù)值相同,那么hashCode必然相同* * Double類(lèi)關(guān)于hashCode源碼說(shuō)明:* Double類(lèi)的hashCode是根據(jù)對(duì)應(yīng)的double值計(jì)算獲得的。* * 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類(lèi)關(guān)于hashCode源碼:* public static int hashCode(double value) {long bits = doubleToLongBits(value);return (int)(bits ^ (bits >>> 32));}*/System.out.println("--------------------原始類(lèi)型對(duì)應(yīng)的包裝類(lèi)-----------------------");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} }總結(jié)
以上是生活随笔為你收集整理的Java源码解析:hashCode与相同对象的关系的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Integer类对象池与==问题:Int
- 下一篇: Java源码解析:深入理解==和equa