日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

Java源码解析:hashCode与相同对象的关系

發(fā)布時(shí)間:2025/3/20 java 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。