我用字符串文字創(chuàng)建了 3 個字符串 2,用 new 創(chuàng)建了一個字符串。但是當我為他們打印哈希碼時,它給出了相同的哈希碼。我很困惑它如何返回相同的哈希碼。請在下面找到示例。public class StringTest{ public static void main(String[] args) { String str = "abc"; String str1 = "hfdjkfhs"; System.out.println("hashValue str:::" + str1.getClass().hashCode()); System.out.println("hashValue str:::" + str.getClass().hashCode()); String str2 = new String("def"); System.out.println("hashValue:::" + str2.getClass().hashCode()); }}輸出 :-hashValue str1:::366712642hashValue str::366712642hashValue str2:::366712642
2 回答

ABOUTYOU
TA貢獻1812條經(jīng)驗 獲得超5個贊
您正在打印String
類的哈希碼,而不是創(chuàng)建的String
對象。
代替:
str.getClass().hashCode()
你應(yīng)該有:
str.hashCode()
添加回答
舉報
0/150
提交
取消