String and intern

1. 在學習JAVA的時候就知道==比較的是內存地址.而equals比較的是內存地址對應的值!(可是還是有很多的人問來問去的,真不知道他們JAVA的基礎課程是怎麼學的?!)

2. JAVA所有的對象都是存放在堆中的!你獲取的"對象"僅僅只是對象的引用而已

3. String是比較特殊的對象,特殊在
3.1 > String a = new String("test") -此時你是在堆中實例化了一個字符串對象
3.2 > String b = "test"-此時JVM會先去堆中尋找這樣的對象;如果有就返回此對象的引用;如果沒有就重新實例化一個這樣的對象!基於這樣的一個過程所以JAVA要求String不可以更改值的。

3.3 >intern()方法就是試圖完成這樣的一個尋找過程
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章