<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    鷹翔宇空

    學習和生活

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      110 Posts :: 141 Stories :: 315 Comments :: 1 Trackbacks
    引自:http://www.mengyan.org/blog/archives/2004/08/14/87.html

    These days, when I’m reading some other guy’s code, I encounter some problem. In these code, I can read following codes:

    1. JButton?button?= new?JButton("OK");
    2. button.setActionCommand("OK");
    3. button.addActionListener(this);
    4. ......
    5. actionPerformed(e)?{
    6. ? ? String?s?= e.getActionCommand();
    7. ? ? if(s=="OK")
    8. ? ? ......
    9. }

    Look, it use “==” but not “equals” here. “Oh, it must be a bug!”, I told myself. But, wait, when I excuted the program, it worked quite well. And till this moment, I remembered, current powerful JVM will maintain a constant pool for String, so maybe two “OK” will reference the same place. So, such mechanism will work.

    But, does Java Language Specification has such standard? and all JVM will follow this standard? I continue to dig into it.

    First, I found such item in JLS 3.10.5 — String literals.

    Abstract some points here:

    Literal strings within the same class ($8) in the same package ($7) represent references to the same String object ($4.3.1).
    Literal strings within different classes in the same package represent references to the same String object.
    Literal strings within different classes in different packages likewise represent references to the same String object.
    Strings computed by constant expressions ($15.28) are computed at compile time and then treated as if they were literals.
    Strings computed at run time are newly created and therefore distinct.
    The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.

    So, this is the feature of String class, every JVM should follow. And, when I read the code of String.java, I found such comments in method “intern“.

    public String intern()
    Returns a canonical representation for the string object.
    A pool of strings, initially empty, is maintained privately by the class String.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~So good:-)

    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.
    It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
    All literal strings and string-valued constant expressions are interned. String literals are defined in $3.10.5 of the Java Language Specification.

    It’s quite clear about the case I encountered. Then I understand, this is not a bug, but the trick :P . But, why he use “==” but not “equals“, I think that will be more clear and more easy to read, I think there must be other guys to be puzzled as me :-) .

    I think the author must care more about the performance than the code, the performance of “==” sure if better than “equals“, especially when the String is long~.

    Ok, everything clear now, and sure, not only String has such pool. For example:

    1. Integer?i?= 1;
    2. Integer?j?= 1;

    what will System.out.println(i==l) output? I think you can get the right answer. :P

    Update (2005.11.09):

    "String"’s method "equalsIgnoreCase" also take advantage of this string pool:

    1. public?boolean?equalsIgnoreCase(String?anotherString)?{
    2. ? ? return?(this?== anotherString)?? true?:
    3. ? ? ? ? ? ?(anotherString?!= null)?&& (anotherString.count?== count)?&&
    4. ? ? regionMatches(true, 0, anotherString, 0, count);
    5. }

    If the two string point the same one, simply return true.

    So, if you need to compare Strings frequently, you should "Intern" them.

    For example, in Lucene’s source code, you need to compare the Field name very often, so, they intern the field name.

    1. this.name?= name.intern();? ? ? ? ? ? ? // field names are interned

    Popularity: 9%

    posted on 2006-09-13 17:45 TrampEagle 閱讀(834) 評論(0)  編輯  收藏 所屬分類: java
    主站蜘蛛池模板: 国产成人精品免费视频网页大全| 一级毛片a女人刺激视频免费| 久久永久免费人妻精品下载| 中文字幕亚洲乱码熟女一区二区| 视频一区在线免费观看| 国产精品无码一区二区三区免费| 苍井空亚洲精品AA片在线播放 | 色www永久免费视频| 在线综合亚洲欧洲综合网站| 四虎在线视频免费观看| 亚洲精品宾馆在线精品酒店| 日本免费的一级v一片| 香蕉视频在线观看免费| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 黑人精品videos亚洲人| 日本亚洲欧洲免费天堂午夜看片女人员| 好看的亚洲黄色经典| 亚洲视频在线免费看| 国产精品亚洲一区二区麻豆| 永久免费AV无码网站在线观看| 一级毛片正片免费视频手机看| 国产亚洲精aa成人网站| 日本免费一区二区三区四区五六区| 久久丫精品国产亚洲av不卡| 大地资源二在线观看免费高清| 国产亚洲情侣久久精品| 亚洲熟伦熟女新五十路熟妇 | 日本高清免费网站| 日韩在线一区二区三区免费视频 | 一级成人a免费视频| 内射干少妇亚洲69XXX| 最近免费中文字幕大全视频 | 一级全免费视频播放| 国产免费久久精品| 亚洲AV色欲色欲WWW| 亚洲热妇无码AV在线播放| 美女视频黄免费亚洲| 一级特级aaaa毛片免费观看 | 91短视频在线免费观看| 亚洲av无码有乱码在线观看| 国产精品亚洲аv无码播放|