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

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

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

    javaGrowing

      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      92 隨筆 :: 33 文章 :: 49 評(píng)論 :: 0 Trackbacks
    What is the performance impact of the StringBuffer and String classes?

    Summary
    Reggie illuminates the underlying performance impact of using the StringBuffer and String classes when performing concatenations. (600 words)
    By Reggie Hutcherson

    Java provides the StringBuffer and String classes, and the String class is used to manipulate character strings that cannot be changed. Simply stated, objects of type String are read only and immutable. The StringBuffer class is used to represent characters that can be modified.

    The significant performance difference between these two classes is that StringBuffer is faster than String when performing simple concatenations. In String manipulation code, character strings are routinely concatenated. Using the String class, concatenations are typically performed as follows:

    String str = new String ("Stanford ");
    str += "Lost!!";

    If you were to use StringBuffer to perform the same concatenation, you would need code that looks like this:

    StringBuffer str = new StringBuffer ("Stanford ");
    str.append("Lost!!");

    Developers usually assume that the first example above is more efficient because they think that the second example, which uses the append method for concatenation, is more costly than the first example, which uses the + operator to concatenate two String objects.

    The + operator appears innocent, but the code generated produces some surprises. Using a StringBuffer for concatenation can in fact produce code that is significantly faster than using a String. To discover why this is the case, we must examine the generated bytecode from our two examples. The bytecode for the example using String looks like this:

    0 new #7
    3 dup
    4 ldc #2
    6 invokespecial #12
    9 astore_1
    10 new #8
    13 dup
    14 aload_1
    15 invokestatic #23
    18 invokespecial #13
    21 ldc #1
    23 invokevirtual #15
    26 invokevirtual #22
    29 astore_1

    The bytecode at locations 0 through 9 is executed for the first line of code, namely:

    String str = new String("Stanford ");

    Then, the bytecode at location 10 through 29 is executed for the concatenation:

    str += "Lost!!";

    Things get interesting here. The bytecode generated for the concatenation creates a StringBuffer object, then invokes its append method: the temporary StringBuffer object is created at location 10, and its append method is called at location 23. Because the String class is immutable, a StringBuffer must be used for concatenation.

    After the concatenation is performed on the StringBuffer object, it must be converted back into a String. This is done with the call to the toString method at location 26. This method creates a new String object from the temporary StringBuffer object. The creation of this temporary StringBuffer object and its subsequent conversion back into a String object are very expensive.

    In summary, the two lines of code above result in the creation of three objects:

    A String object at location 0
    A StringBuffer object at location 10
    A String object at location 26
    Now, let's look at the bytecode generated for the example using StringBuffer:

    0 new #8
    3 dup
    4 ldc #2
    6 invokespecial #13
    9 astore_1
    10 aload_1
    11 ldc #1
    13 invokevirtual #15
    16 pop

    The bytecode at locations 0 to 9 is executed for the first line of code:

    StringBuffer str = new StringBuffer("Stanford ");

    The bytecode at location 10 to 16 is then executed for the concatenation:

    str.append("Lost!!");

    Notice that, as is the case in the first example, this code invokes the append method of a StringBuffer object. Unlike the first example, however, there is no need to create a temporary StringBuffer and then convert it into a String object. This code creates only one object, the StringBuffer, at location 0.

    In conclusion, StringBuffer concatenation is significantly faster than String concatenation. Obviously, StringBuffers should be used in this type of operation when possible. If the functionality of the String class is desired, consider using a StringBuffer for concatenation and then performing one conversion to String.

    About the author
    Reggie Hutcherson is a Sun technology evangelist. He evangelizes Sun's Java 2 Platform technologies around the world concentrating on J2SE and the HotSpot performance engine.

    Resources

    "JavaWorld debuts new weekly Java performance column," Reggie Hutcherson (JavaWorld, March 2000):
    http://www.javaworld.com/jw-03-2000/jw-03-javaperf.html

    "The basics of Java performance," Reggie Hutcherson (JavaWorld, March 2000):
    http://www.javaworld.com/jw-03-2000/jw-03-javaperf_2.html

    "Performance problem or design problem?" Reggie Hutcherson (JavaWorld, March 2000):
    http://www.javaworld.com/jw-03-2000/jw-03-javaperf_3.html

    "Compiler optimizations," Reggie Hutcherson (JavaWorld, March 2000):
    http://www.javaworld.com/jw-03-2000/jw-03-javaperf_4.html

    posted on 2005-09-12 09:25 javaGrowing 閱讀(434) 評(píng)論(0)  編輯  收藏 所屬分類: 好文收藏
    主站蜘蛛池模板: 国产美女被遭强高潮免费网站| 免费国产99久久久香蕉| 成人无遮挡裸免费视频在线观看 | 亚洲伊人久久大香线蕉啊| 一级毛片aaaaaa免费看| 久久亚洲国产视频| 一区二区三区观看免费中文视频在线播放 | 免费无码一区二区三区蜜桃大| 亚洲依依成人精品| 国产卡一卡二卡三免费入口| 亚洲一区在线观看视频| 久久久久国产精品免费免费搜索| 亚洲一区欧洲一区| 免费一级毛片在播放视频| 免费的黄网站男人的天堂| 国产L精品国产亚洲区久久 | 亚洲精品国产成人片| 日本免费大黄在线观看| 亚洲国产一区在线观看| 妞干网在线免费视频| 羞羞视频在线观看免费| 亚洲精品无码mv在线观看网站| 久久精品私人影院免费看| 亚洲国产精品日韩在线观看 | 在线观看特色大片免费网站| 亚洲天堂在线播放| 成人免费看片又大又黄| 九九九国产精品成人免费视频| 亚洲AV天天做在线观看| 在线视频观看免费视频18| 青青青亚洲精品国产| 亚洲国产精品无码专区在线观看| 亚洲最大免费视频网| 大桥未久亚洲无av码在线 | 伊伊人成亚洲综合人网7777| 久久99国产乱子伦精品免费| 亚洲乱码在线卡一卡二卡新区| 亚洲精品国产高清嫩草影院| 久久免费动漫品精老司机| 亚洲色大成网站www永久男同| 老司机亚洲精品影视www|