java 板


LINE

在 Object 的說明中有一段不是很確定它的意思, 想請教一下: public int hashCode() ... As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java programming language.) 這段話指的是, 在可行的狀況下, Object 的 hashCode() 會傳回 unique 的值嗎? 例如, 32-bit JVM 中, 至少一個可行的方法是, 籍由傳回 object 的 address 達到傳回 unique 的值, 所以 32-bit JVM 中 Object 的 hashCode() 傳回的是 unique 的值. 這個 unique 傳回值通常是 address 但不保證是 address. 但 64-bit JVM 中, 沒有可行的方法保證不同 object 可以傳回不同的值, (因為 address 範圍超過 integer 範圍) 所以 64-bit JVM 中 Object 的 hashCode() 傳回的不一定是 unique 的值. 這樣的理解正確嗎? --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.126.30.95
1F:推 james732:Effective Java前幾章關於hashCode, equals的討論可以看 01/13 18:46
2F:→ RockLee:請問您指的是這章嗎? 01/13 19:12
4F:→ RockLee:Chapter3.pdf 01/13 19:14
5F:→ RockLee:不好意思我是有看到一些關於hashCode, equals的討論, 01/13 19:16
6F:→ RockLee:但並沒找到哪邊講到Object的hashCode()是如何實作的, 01/13 19:17
7F:→ RockLee:可否更明確地指出在哪邊呢? 謝謝~ 01/13 19:18
8F:→ james732:書裡沒有講實作方式,只有原則,什麼時候應該相同或不同 01/13 20:11
9F:→ RockLee:了解, 那回到原來問題, 在可行的狀況下 (ex. 32-bit JVM), 01/13 20:21
10F:→ RockLee:Object 的 hashCode() 實作是否一定會傳回 unique 的值? 01/13 20:22
11F:→ RockLee:有人發現過您用的 JVM 不是傳回 unique 的值嗎? 01/13 20:23
12F:→ adrianshum:簡單一句:不該當hashCode會回傳unique值。這非其本意 01/13 22:40
13F:→ RockLee:我了解 hashCode() 的意義, 但這並不是我的問題… 01/13 23:14
14F:→ RockLee:因為 Object 的說明中提到: 01/13 23:17
15F:→ RockLee:However, the programmer should be aware that 01/13 23:19
16F:→ RockLee:producing distinct integer results for unequal objects 01/13 23:21
17F:→ RockLee:may improve the performance of hashtables. 01/13 23:23
18F:→ RockLee:所以我才想確認 Object 原始提供的 hashCode() 實作為何? 01/13 23:24
20F:→ RockLee:lang/Object.html 01/13 23:26
21F:→ qrtt1:那就去翻 source code 啊,翻手冊做什麼@@ 01/14 00:19
22F:推 pzyc79:source code只會給你public native int hashCode();這行字 01/14 00:37
23F:→ pzyc79:我想知道這種原生函式的原始碼去哪裡可以找到@@a; 01/14 00:42
24F:→ RockLee:可是我的問題是, As much as is reasonably practical ... 01/14 00:45
25F:→ RockLee:這段話是否要求所有 JVM 的實作, 在可行的狀況下, 01/14 00:47
26F:→ RockLee:Object 的 hashCode() 傳回 unique 的值? 01/14 00:48
27F:→ RockLee:似乎不是翻某個 JVM 的 source code 可以解決的... 01/14 00:49
28F:→ pzyc79:就算hashcode真的是unique你也不能把當unique用… 01/14 01:10
29F:推 PsMonkey:太難的事情我不懂,我只想問:你怎麼知道沒有被 override 01/14 16:13
30F:→ tkcn:原 po 應該是單純指 "Object" 這個類別的實體 01/14 16:21
31F:推 ClareQ:不可能是unique,且Object預設hash實作與記憶體位置有關 01/14 16:49
32F:→ RockLee:我問的的確只是"Object"這個類別的hashCode() 01/14 20:27
33F:→ RockLee:為什麼不可能是unique呢? 01/14 20:29
34F:→ RockLee:如果回傳記憶體位置至少32-bit JVM就可以達到unique了不是 01/14 20:31
35F:推 PsMonkey:要是我,我也不會跟你保證是 unique... 01/15 13:21







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP