我意識到變量名的 hashCode 在“更新”后是不同的,但客觀上是什么使得 String 對象實際上是不可變的?public static void main(String[] args) {? ? ? ? String str = "AB";? ? ? ? ?System.out.println(str ); // AB? ? ? ? ?str = str .replace(str .charAt(0) ,'W');? ? ? ? ?System.out.println(str );//WB?}編輯1:hashCode基于變量的值,與內存地址無關。編輯2:我現在明白字符串是引用,而不是它本身的對象。
1 回答
翻閱古今
TA貢獻1780條經驗 獲得超5個贊
如果您查看 的文檔replace(),它會提到:
返回一個新字符串,該字符串是用 newChar 替換該字符串中所有出現的 oldChar 所得的結果。
因此,替換后的String是一個全新的String。
添加回答
舉報
0/150
提交
取消
