Re: [問題] 請教String的問題
非常感謝大家的熱心回應,我完全理解了
現在只剩一個小問題想問...
ex1:
String a = "Hello";
a = "kdok123";
這個結果是new了兩個String的空間在Heap
ex2:
Interger a = 3;
a = 4;
結果是new了一個Interger的空間在Heap,a指向的值從3改成了4
以上兩個例子體現了String的不可變性
請問例子的觀念是對的嗎?
另外還有一個小疑惑:為什麼java要定義String的不可變性呢?
除了不斷的增加heap和GC的負擔之外我想不到其他好處欸...
---------------------------------------------------------
補充:想了一下我好像還沒完全理解
以dark大&Chikei大的觀念來看我的ex1:
輸出兩次會是一樣的原因是因為String的不可變性
"所以str2指向的地方因為不可改變,只能另外再開一個空間去存新的值"
但以這樣的觀念把String改成Integer,似乎就應該得到改變過後的結果
(因為Integer可以變)
但實際測試發現Interger得到的結果也是兩個一樣的輸出
到底是為什麼呢?
2次補充: java的所有 primitive wrapper class都是 immutable的
https://en.wikipedia.org/wiki/Primitive_wrapper_class
所以這樣觀念就完全通了
補充一點: 上面所有提到call by value的大大, java只有call by value是沒錯的
但如果是以String或是Integer傳入的value會是address,而不像一般傳入primitive
的value
所以ex1的call by value會新複製一個address指向本來的位置,
這個時候的修改應該是可以成功的
但因為此時Immutable的特性,所以會新開一份空間來存值,才保留了原本的值
若沒有這樣的特性,值是會變的,跟call by value沒有關係
以上是我整理的觀念,若有錯誤煩請討論指正
再次感謝大家的熱心回應!
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.250.185.98
※ 文章網址: http://www.ptt.cc/bbs/java/M.1418904044.A.327.html
※ 編輯: kdok123 (60.250.185.98), 12/18/2014 20:12:09
※ 編輯: kdok123 (60.250.185.98), 12/18/2014 20:15:14
→
12/18 20:26, , 1F
12/18 20:26, 1F
→
12/18 20:28, , 2F
12/18 20:28, 2F
→
12/18 20:33, , 3F
12/18 20:33, 3F
→
12/18 20:34, , 4F
12/18 20:34, 4F
→
12/18 20:35, , 5F
12/18 20:35, 5F
※ 編輯: kdok123 (61.221.50.98), 12/18/2014 20:49:04
→
12/18 20:39, , 6F
12/18 20:39, 6F
→
12/18 20:40, , 7F
12/18 20:40, 7F
→
12/18 20:43, , 8F
12/18 20:43, 8F
→
12/18 20:44, , 9F
12/18 20:44, 9F
→
12/18 20:45, , 10F
12/18 20:45, 10F
→
12/18 20:45, , 11F
12/18 20:45, 11F
→
12/18 20:47, , 12F
12/18 20:47, 12F
→
12/18 20:52, , 13F
12/18 20:52, 13F
→
12/18 20:52, , 14F
12/18 20:52, 14F
→
12/18 20:52, , 15F
12/18 20:52, 15F
→
12/18 20:53, , 16F
12/18 20:53, 16F
→
12/18 20:54, , 17F
12/18 20:54, 17F
→
12/18 20:55, , 18F
12/18 20:55, 18F
→
12/18 20:55, , 19F
12/18 20:55, 19F
→
12/18 20:56, , 20F
12/18 20:56, 20F
→
12/18 20:56, , 21F
12/18 20:56, 21F
→
12/18 20:57, , 22F
12/18 20:57, 22F
→
12/18 20:57, , 23F
12/18 20:57, 23F
→
12/18 20:57, , 24F
12/18 20:57, 24F
→
12/18 20:57, , 25F
12/18 20:57, 25F
→
12/18 20:57, , 26F
12/18 20:57, 26F
→
12/18 20:59, , 27F
12/18 20:59, 27F
→
12/18 21:02, , 28F
12/18 21:02, 28F
→
12/18 21:02, , 29F
12/18 21:02, 29F
→
12/18 21:03, , 30F
12/18 21:03, 30F
推
12/18 21:05, , 31F
12/18 21:05, 31F
→
12/18 21:06, , 32F
12/18 21:06, 32F
→
12/18 21:07, , 33F
12/18 21:07, 33F
→
12/18 21:10, , 34F
12/18 21:10, 34F
→
12/18 21:14, , 35F
12/18 21:14, 35F
→
12/18 21:15, , 36F
12/18 21:15, 36F
→
12/18 21:17, , 37F
12/18 21:17, 37F
→
12/18 21:17, , 38F
12/18 21:17, 38F
推
12/18 21:23, , 39F
12/18 21:23, 39F
→
12/18 21:43, , 40F
12/18 21:43, 40F
→
12/18 21:43, , 41F
12/18 21:43, 41F
→
12/18 21:43, , 42F
12/18 21:43, 42F
→
12/18 21:44, , 43F
12/18 21:44, 43F
→
12/18 21:45, , 44F
12/18 21:45, 44F
→
12/18 23:20, , 45F
12/18 23:20, 45F
→
12/18 23:22, , 46F
12/18 23:22, 46F
推
12/18 23:37, , 47F
12/18 23:37, 47F
→
12/18 23:37, , 48F
12/18 23:37, 48F
→
12/18 23:38, , 49F
12/18 23:38, 49F
→
12/18 23:38, , 50F
12/18 23:38, 50F
→
12/18 23:46, , 51F
12/18 23:46, 51F
→
12/18 23:49, , 52F
12/18 23:49, 52F
→
12/18 23:50, , 53F
12/18 23:50, 53F
→
12/18 23:51, , 54F
12/18 23:51, 54F
→
12/18 23:51, , 55F
12/18 23:51, 55F
→
12/18 23:53, , 56F
12/18 23:53, 56F
推
12/19 07:28, , 57F
12/19 07:28, 57F
→
12/19 09:22, , 58F
12/19 09:22, 58F
→
12/19 09:22, , 59F
12/19 09:22, 59F
→
12/19 09:23, , 60F
12/19 09:23, 60F
→
12/19 09:24, , 61F
12/19 09:24, 61F
→
12/19 09:25, , 62F
12/19 09:25, 62F
→
12/19 09:25, , 63F
12/19 09:25, 63F
→
12/19 09:41, , 64F
12/19 09:41, 64F
→
12/19 09:42, , 65F
12/19 09:42, 65F
→
12/19 09:43, , 66F
12/19 09:43, 66F
→
12/19 09:44, , 67F
12/19 09:44, 67F
→
12/19 09:45, , 68F
12/19 09:45, 68F
→
12/19 09:48, , 69F
12/19 09:48, 69F
→
12/19 09:48, , 70F
12/19 09:48, 70F
→
12/19 09:50, , 71F
12/19 09:50, 71F
→
12/19 09:51, , 72F
12/19 09:51, 72F
→
12/19 10:05, , 73F
12/19 10:05, 73F
→
12/19 10:06, , 74F
12/19 10:06, 74F
→
12/19 10:07, , 75F
12/19 10:07, 75F
→
12/19 10:10, , 76F
12/19 10:10, 76F
→
12/19 10:11, , 77F
12/19 10:11, 77F
→
12/19 10:11, , 78F
12/19 10:11, 78F
→
12/19 10:22, , 79F
12/19 10:22, 79F
→
12/19 10:22, , 80F
12/19 10:22, 80F
→
12/19 10:24, , 81F
12/19 10:24, 81F
討論串 (同標題文章)
java 近期熱門文章
PTT數位生活區 即時熱門文章