看板 java 關於我們 聯絡資訊
public class test02 { public static void main(String args[]) { String a = new String("abc"); String b = new String("abc"); String c = "abc"; String d = "abc"; String e = "def"; String f = "abcdef"; String g = c+e; String h = (c+e).intern(); String i = new String("abc").intern(); System.out.println(a == b); // false System.out.println(c == d); // true System.out.println(f == g); // false System.out.println(f == h); // true } } 這是一段在網路上看到關於string pool的範例 我想問的是: 1.第一個true是怎麼來的? 為什麼會指向同一個"abc"?(因為上面創造了兩個) 2.裡面的g是同時指向"abc"和"def"的位址嗎? 3.假設不看這支程式String a = new String("abc") 和 String a = "abc" 的不同在哪?是一個是製造兩個物件,另一個只有製造一個? 雖然有爬過文章但是還是不甚了解 拜託各位指教了!!謝謝!! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.119.135.223 ※ 編輯: TKforce 來自: 140.119.135.223 (01/16 23:58)
TonyQ:有new的會重新配置空間 , 沒有的就是進到 string pool ,下一 01/16 23:59
tkcn: 次用到就可以直接從 string pool 拿. 01/17 00:30
TKforce:所以用new的不會進到string pool裡面囉? 01/17 00:40
hilorrk:沒有人覺得這推文怪怪的嗎XD 01/17 10:35
dendrobium:推文接龍嗎XD? 01/17 12:54
petertc:runtime建立的String除非用intern(),否則都不會記在pool? 01/17 15:42
TonyQ:(奇怪,我昨天明明推了一行就忘了回來推了(思)) 01/17 21:52
tkcn:哈,我看過了半小時應該是忘了,我還揣摩了一陣子看要怎麼接 01/17 23:17
puzi:樓上的大大真有趣XD 01/18 23:35