Re: [問題] 關於 "字串 ==" 的用法
※ 引述《homal (\冏/)》之銘言:
請參考這篇
http://tinyurl.com/4qkf8h6
來自良葛格的學習筆記
http://caterpillar.onlyfun.net/Gossip/JavaGossip-V1/ImmutableString.htm
: 想請教一下,
: String x = "hello";
: String y = "hello";
: System.out.println( x==y);
: 電腦輸出 true
: 為何會這樣呢?!
這是因為JVM內部維護的字串池的緣故
宣告 變數y 時,沒有產生第二個 "hello" 物件,
y物件參考到內容為"hello"的字串物件,是來自字串池內已經有的"hello"物件
所以用 == 比較是否參考到同一個物件時,會得到 true
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.231.145.200
討論串 (同標題文章)