strings are constant; their values cannot be changed after they are created. string buffers support mutable strings. because string objects are immutable they can be shared.
字串是恆定的,建立之後它們的值不能被改變。stringbuffer是可變的strings.字串物件不可變讓它們可以被共享。
string a =
"abc";
log.e("ss","a之前的hashcode:"
+a.hashcode());//a之前的hashcode:96354
a ="abc"
+"123";
log.e("ss","a之後的hashcode:"
+a.hashcode());//a之後的hashcode:-1424436592
String為什麼要設計成final
strings are constant their values cannot be changed after they are created.string buffers support mutable strings.because string objects are immutable...
String 為什麼要設計成不可變的
string不可變 string值儲存在常量池中。乙個已有字串 abcd 如果改變值成 abcedl 不是在原記憶體位址上修改資料,而是在常量池中查詢或生成這個新值,再把引用指向新值的位址,如圖 string 類本身是final的,不可以被繼承 string類內部通過private final ch...
String類為什麼被設計成不可變類
1.共享元素模式,也就是說 乙個系統中如果有多處用到了相同的乙個元素,那麼我們應該只儲存乙份此元素,而讓所有地方都引用這乙個元素。2.j a中string就是根據享元模式設計的,而那個儲存元素的地方就叫做 字串常量池 string pool public class public static vo...