漁人碼頭

          天行健,君子以自強(qiáng)不息。地勢(shì)坤,君子以厚德載物。
          posts - 12, comments - 16, trackbacks - 0, articles - 43
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          intern()得到的String之間的==

          Posted on 2007-03-11 21:09 Fisher 閱讀(481) 評(píng)論(0)  編輯  收藏 所屬分類: Java 學(xué)習(xí)筆記
          String s1="abc"; // 這種形式的字符串,被保存在常量池中
          String s2=new String("abc"); // 用new生成的字符串,保存在堆中
          關(guān)于intern()方法,見(jiàn)下列說(shuō)明:
          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.
          綜上,解決你的第三個(gè)問(wèn)題:
          String s1=new String("abc");     // s1指向的是堆中的地址
          String s2=new String("abc").intern();  // s2指向的是常量池中的地址
          System.out.println(s1==s2) ;    // 地址根本就不同,自然s1==s2為false"

          ?

          ????????String?aaa?=?new?String("aaa");
          ????????String?aaa2?
          =?new?String("aaa").intern();
          ????????
          ????????System.out.println(aaa?
          ==?aaa2);//false
          ????????System.out.println(aaa?==?"aaa");//false
          ????????System.out.println(aaa2?==?"aaa");//true
          主站蜘蛛池模板: 宁津县| 淮安市| 陆良县| 仁怀市| 增城市| 汤阴县| 泾阳县| 合肥市| 康马县| 来宾市| 大荔县| 当阳市| 米脂县| 交城县| 襄垣县| 萝北县| 蒙山县| 余干县| 南城县| 阿拉善左旗| 镇平县| 胶州市| 兴安盟| 肥乡县| 安国市| 晴隆县| 普宁市| 宿州市| 兴安盟| 广安市| 吉林市| 兴宁市| 利辛县| 麻江县| 达拉特旗| 搜索| 福安市| 涪陵区| 阿克苏市| 烟台市| 沙坪坝区|