<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                在jvm規范中,每個類型都有自己的常量池。常量池是某類型所用常量的一個有序集合,包括直接常量(基本類型,String)和對其他類型、字段、方法的符號引用。之所以是符號引用而不是像c語言那樣,編譯時直接指定其他類型,是因為java是動態綁定的,只有在運行時根據某些規則才能確定具體依賴的類型實例,這正是java實現多態的基礎。 為了對常量池有更具體的認識,下面引用幾個例子: ## 1 常量池中對象和堆中的對象 public?class?Test{ Integer?i1=new?Integer(1); ???Integer?i2=new?Integer(1); //i1,i2分別位于堆中不同的內存空間 ???System.out.println(i1==i2);//輸出false ???Integer?i3=1; ???Integer?i4=1; //i3,i4指向常量池中同一個內存空間 ???System.out.println(i3==i4);//輸出true //很顯然,i1,i3位于不同的內存空間 System.out.println(i1==i3);//輸出false } ## 2 8種基本類型的包裝類和對象池 java中基本類型的包裝類的大部分都實現了常量池技術,這些類是Byte,Short,Integer,Long,Character,Boolean,另外兩種浮點數類型的包裝類則沒有實現。另外Byte,Short,Integer,Long,Character這5種整型的包裝類也只是在對應值小于等于127時才可使用對象池,也即對象不負責創建和管理大于127的這些類的對象。以下是一些對應的測試代碼: public?class?Test{ public?static?void?main(String[]?args){ ???//5種整形的包裝類Byte,Short,Integer,Long,Character的對象, ???//在值小于127時可以使用常量池 ???Integer?i1=127; ???Integer?i2=127; ???System.out.println(i1==i2)//輸出true ???//值大于127時,不會從常量池中取對象 ???Integer?i3=128; ???Integer?i4=128; ???System.out.println(i3==i4)//輸出false ???//Boolean類也實現了常量池技術 ???Boolean?bool1=true; ???Boolean?bool2=true; ???System.out.println(bool1==bool2);//輸出true ???//浮點類型的包裝類沒有實現常量池技術 ???Double?d1=1.0; ???Double?d2=1.0; ???System.out.println(d1==d2)//輸出false ?? } } ## 3 String也實現了常量池技術 String類也是java中用得多的類,同樣為了創建String對象的方便,也實現了常量池的技術,測試代碼如下: public?class?Test{ public?static?void?main(String[]?args){ //s1,s2分別位于堆中不同空間 String?s1=new?String("hello"); String?s2=new?String("hello"); System.out.println(s1==s2)//輸出false //s3,s4位于池中同一空間 String?s3="hello"; String?s4="hello"; System.out.println(s3==s4);//輸出true } } ## 4 字符串比較更豐富的一個例子 ~~~ package?testPackage; class?Test?{ ?public?static?void?main(String[]?args)?{ ??String?hello?=?"Hello",?lo?=?"lo"; ??System.out.print((hello?==?"Hello")?+?"?");//1 ??System.out.print((Other.hello?==?hello)?+?"?");//2 ??System.out.print((other.Other.hello?==?hello)?+?"?");//3 ??System.out.print((hello?==?("Hel"+"lo"))?+?"?");//4 ??System.out.print((hello?==?("Hel"+lo))?+?"?");//5 ??System.out.println(hello?==?("Hel"+lo).intern());//6 ?} } class?Other?{?static?String?hello?=?"Hello";?} ~~~ ~~~ and?the?compilation?unit: package?other; public?class?Other?{?static?String?hello?=?"Hello";?} produces?the?output: true?//1 true?//2 true?//3 true?//4 false?//5 true //6 ~~~ 輸出結果的分別解釋如下: 在同包同類下,引用自同一String對象. 在同包不同類下,引用自同一String對象. 在不同包不同類下,依然引用自同一String對象 在編譯成.class時能夠識別為同一字符串的,自動優化成常量,所以也引用自同一String對象 在運行時創建的字符串具有獨立的內存地址,所以不引用自同一String對象 String的intern()方法會查找在常量池中是否存在一份equal相等的字符串,如果有則返回一個引用,沒有則添加自己的字符串進進入常量池, 注意,只是字符串部分, 所以這時會存在2份拷貝,常量池的部分被String類私有持有并管理,自己的那份按對象生命周期繼續使用.
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看