Java基礎(chǔ)問題:java.lang.String - intern()

          Posted on 2006-03-02 20:56 killvin 閱讀(929) 評(píng)論(0)  編輯  收藏 所屬分類: java

          原文: http://forum.javaeye.com/viewtopic.php?t=17912


          摟主的問題問的含含糊糊:flyjie給出了非常詳細(xì)的解釋,
          不過就是沒有解釋String實(shí)例化的特殊方面以及Intern()方法的含義

          -------
          ---------------------------------------------------------------------------------
          ---------------------------------------------------------------------------------
          java代碼:
          String str ;

          這樣聲明str它只支是一個(gè)對(duì)象的reference,不會(huì)產(chǎn)生實(shí)際的對(duì)象。如果沒有初始化str,編譯時(shí)便會(huì)發(fā)生錯(cuò)誤。
          java代碼:
          String str1=new String("test");
          String str2 = "test";

          str1是一個(gè)新的對(duì)象。new關(guān)鍵字的意思就是創(chuàng)建某個(gè)新的對(duì)象。而str2是一個(gè)對(duì)象的引用。 它們的內(nèi)容相同,但內(nèi)存地址是不一樣的。 java中對(duì)象的引用存在Stack(棧)中,而對(duì)象由Heap(堆)分配空間。

          3、引用==變量?  不一定
          java代碼:

            public class TestString {
          public static void main(String[] args) {
          String s1 = "test";
          String s2 = new String("test");
          if (s1 == s2)
          System.out.println("s1 == s2");
          else
          System.out.println("s1 != s2");
          if (s1.equals(s2))
          System.out.println("s1 equals s2");
          else System.out.println("s1 not equals s2");
          }
          }

          我們將 s2 用 new 操作符創(chuàng)建程序輸出:s1 != s2 s1 equals s2.
          java代碼:

          s2 = s2.intern();

          在你加上這句話后,上面的程序輸入:s1 == s2 s1 equals s2

          而String a = "test" ; String b = "test" ; a == b 會(huì)返回true; 這里a="test"時(shí)創(chuàng)建一個(gè)在棧中的reference, b=test時(shí)jvm發(fā)現(xiàn)棧中已存在名為"test"的字符串,直接引用。結(jié)論:String 是個(gè)對(duì)象,要對(duì)比兩個(gè)不同的String對(duì)象的值是否相同明顯的要用到 equals() 這個(gè)方法. 而== 比較的是內(nèi)存地址的值。

          4、private final String a = "test", 這個(gè)a屬于常量,存放在常量存儲(chǔ)空間(CS)中。

          5、建議你看看<<深入淺出java虛擬機(jī)>>一書。

          -------
          ---------------------------------------------------------------------------------
          ---------------------------------------------------------------------------------



          總結(jié)

          1. 在學(xué)習(xí)JAVA的時(shí)候就知道==比較的是內(nèi)存地址.而equals比較的是內(nèi)存地址對(duì)應(yīng)的值!(可是還是有很多的人問來(lái)問去的,真不知道他們JAVA的基礎(chǔ)課程是怎么學(xué)的?!)

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

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

          3.3 >intern()方法就是試圖完成這樣的一個(gè)尋找過程
          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.

          這里有一份詳細(xì)的參考資料:

          關(guān)于Java棧與堆的思考 http://www.javafan.net/article/20051123115654293.html


          主站蜘蛛池模板: 涞水县| 河北省| 临澧县| 合江县| 沂南县| 淮北市| 四会市| 玛纳斯县| 新龙县| 武城县| 永兴县| 镇安县| 四子王旗| 桐梓县| 镇巴县| 蒙自县| 晋宁县| 翁源县| 仙游县| 定远县| 诸暨市| 白沙| 达日县| 嵩明县| 光泽县| 江西省| 静海县| 石首市| 大城县| 平南县| 金平| 平湖市| 恩平市| 迁西县| 全州县| 姚安县| 临安市| 南靖县| 黄平县| 武隆县| 繁峙县|