[問題] 請教String的問題
ex1:
public static void main(String[] args){
String str1 = "Hello";
System.out.println(str1);
tell(str1);
System.out.println(str1);
}
public static void tell(String str2){
str2 = "kdok123";
}
輸出: Hello
Hello
結論: 因為String的值不能被修改
ex2:
class Ex2{
String temp = "Hello";
}
public class Ex2Demo{
public static void main(String[] args){
Ex2 e1 = new Ex2();
e1.temp = "kdok";
System.out.println(e1.temp);
tell(e1);
System.out.println(e1.temp);
}
public static void tell(Ex2 str2){
str2.temp = "kdok123";
}
}
輸出:kdok
kdok123
問題:為什麼這邊的String就可以被修改呢?
java新手,問題有點淺,我覺得印出來的應該是新new出來的String,並不是原本那一個
但還是不太確定,希望有人可以幫我解釋一下
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.120.191.244
※ 文章網址: http://www.ptt.cc/bbs/java/M.1418822389.A.AC6.html
※ 編輯: kdok123 (59.120.191.244), 12/17/2014 21:20:14
※ 編輯: kdok123 (59.120.191.244), 12/17/2014 21:21:03
→
12/17 21:25, , 1F
12/17 21:25, 1F
→
12/17 21:28, , 2F
12/17 21:28, 2F
→
12/17 21:28, , 3F
12/17 21:28, 3F
→
12/17 21:42, , 4F
12/17 21:42, 4F
推
12/17 22:02, , 5F
12/17 22:02, 5F
→
12/17 22:02, , 6F
12/17 22:02, 6F
※ 編輯: kdok123 (220.228.150.131), 12/17/2014 22:04:28
推
12/17 22:06, , 7F
12/17 22:06, 7F
→
12/17 23:03, , 8F
12/17 23:03, 8F
→
12/17 23:08, , 9F
12/17 23:08, 9F
→
12/17 23:16, , 10F
12/17 23:16, 10F
推
12/17 23:20, , 11F
12/17 23:20, 11F
→
12/17 23:21, , 12F
12/17 23:21, 12F
→
12/17 23:22, , 13F
12/17 23:22, 13F
→
12/17 23:31, , 14F
12/17 23:31, 14F
→
12/17 23:31, , 15F
12/17 23:31, 15F
→
12/17 23:31, , 16F
12/17 23:31, 16F
→
12/17 23:36, , 17F
12/17 23:36, 17F
→
12/17 23:36, , 18F
12/17 23:36, 18F
→
12/18 05:49, , 19F
12/18 05:49, 19F
→
12/18 05:49, , 20F
12/18 05:49, 20F
→
12/18 05:50, , 21F
12/18 05:50, 21F
→
12/18 05:51, , 22F
12/18 05:51, 22F
→
12/18 05:52, , 23F
12/18 05:52, 23F
→
12/18 05:52, , 24F
12/18 05:52, 24F
→
12/18 05:53, , 25F
12/18 05:53, 25F
→
12/18 05:54, , 26F
12/18 05:54, 26F
→
12/18 14:10, , 27F
12/18 14:10, 27F
討論串 (同標題文章)
java 近期熱門文章
PTT數位生活區 即時熱門文章