漁人碼頭

          天行健,君子以自強不息。地勢坤,君子以厚德載物。
          posts - 12, comments - 16, trackbacks - 0, articles - 43
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

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

          Posted on 2007-03-11 21:09 Fisher 閱讀(477) 評論(0)  編輯  收藏 所屬分類: Java 學習筆記
          String s1="abc"; // 這種形式的字符串,被保存在常量池中
          String s2=new String("abc"); // 用new生成的字符串,保存在堆中
          關于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.
          綜上,解決你的第三個問題:
          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
          主站蜘蛛池模板: 成安县| 嘉峪关市| 利川市| 阿拉善左旗| 濮阳市| 永吉县| 沅江市| 墨玉县| 安宁市| 来安县| 博乐市| 林甸县| 元氏县| 应用必备| 托里县| 巩义市| 延寿县| 北碚区| 枣庄市| 岳普湖县| 济南市| 武山县| 鱼台县| 琼结县| 探索| 嘉善县| 团风县| 弥勒县| 青海省| 光山县| 龙州县| 富民县| 莫力| 额济纳旗| 东平县| 司法| 青龙| 黔江区| 临清市| 平潭县| 隆德县|