作者darkk6 (Mr. Pan)
看板java
標題Re: [問題] 請教String的問題
時間Thu Dec 18 15:34:58 2014
問題和 String 的 Immutable 比關係較無關(應該說較少),主要是 Method 的
Pass by value 問題
底下用簡單的圖解來表示變數與記憶體之間的關係:
====================================================================
public static void main(String[] args){
String str1 = "Hello";
System.out.println(str1);
str1 ──┐
tell(str1);
↓
System.out.println(str1);
┌───┐
}
│Hello │
└───┘
public static void tell(String str2){
str2 = "kdok123";
}
====================================================================
public static void main(String[] args){
String str1 = "Hello";
System.out.println(str1); str1 ──┐
tell(str1); ↓
System.out.println(str1); ┌───┐
} │Hello │
└───┘
//傳遞給 tell 的 是這個 "Hello" String 的位置 ↑
public static void tell(String str2){
str2 ────┘
str2 = "kdok123";
}
====================================================================
public static void main(String[] args){
String str1 = "Hello";
System.out.println(str1); str1 ──┐
tell(str1); ↓
System.out.println(str1); ┌───┐
} │Hello │
└───┘
//傳遞給 tell 的 是這個 "Hello" String 的位置 ┌────┐
public static void tell(String str2){ str2 ───
──→│kdok123 │
str2 = "kdok123"; └────┘
}
// 因為字串的 Immutable 所以會
// 指向另一個物件
====================================================================
回到 main ,你看到 str1 指向哪個物件呢 ?
接著看 ex2 : ┌──┐
┌─────┐│kdok│
public static void main(String[] args){
│Object Ex2│└──┘
Ex2 e1 = new Ex2(); e1 ──→╞═════╡ ↑
e1.temp = "kdok"; │ temp ─┼─┘
System.out.println(e1.temp);
└─────┘
tell(e1);
System.out.println(e1.temp);
}
public static void tell(Ex2 str2){
str2.temp = "kdok123";
}
============================================================================
┌──┐
┌─────┐│kdok│
public static void main(String[] args){ │Object Ex2│└──┘
Ex2 e1 = new Ex2(); e1 ──→╞═════╡ ↑
e1.temp = "kdok";
┌───→│ temp ─┼─┘
System.out.println(e1.temp);
│ └─────┘
tell(e1); │
System.out.println(e1.temp);
│
}
│
│
public static void tell(Ex2 str2){
│
str2.temp = "kdok123";
str2 ┘
}
============================================================================
┌──┐
┌─────┐│kdok│
public static void main(String[] args){ │Object Ex2│└──┘
Ex2 e1 = new Ex2(); e1 ──→╞═════╡
e1.temp = "kdok"; ┌───→│ temp
─┼─┐
System.out.println(e1.temp); │ └─────┘
│
tell(e1); │
↓
System.out.println(e1.temp); │
┌────┐
} │
│kdok123 │
│
└────┘
public static void tell(Ex2 str2){ │
str2.temp = "kdok123"; str2 ┘
}
// 因為字串的 Immutable 所以會
// 指向另一個物件
============================================================================
回到 main , 你看到 e1.temp 是指向哪個物件呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 218.164.118.189
※ 文章網址: http://www.ptt.cc/bbs/java/M.1418888100.A.52D.html
推 kdok123: 感謝! 這邊我有理解,可是我沒有理解我的問題(上一篇) 12/18 15:40
→ kdok123: 若是字串的immutable特性,那為什麼改成Integer還是跟 12/18 15:40
→ kdok123: String有一樣的結果呢?(ex1) 12/18 15:40
推 LaPass: 樓上,妳沒看懂這一篇,看懂就能理解了。 12/18 15:42
→ LaPass: 所謂的「不能修改」不是指「不能修改那個變數」 12/18 15:43
→ LaPass: 先搞懂 pass by reference、pass by value 這個觀念 12/18 15:44
→ LaPass: 再來看這個,會比較好懂 12/18 15:44
推 LaPass: 還有那不是String的特性,你把它改成int的結果應該會一樣 12/18 15:47
→ bleed1979: 不對人但對樓上,換int和換Integer應該是不同的。 12/19 07:23