[转载]StringBuffer versus String

Java provides the StringBuffer and String classes, and theString 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 StringBufferis 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 twoString 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 usingString looks like this:

0 new #7 <Class java.lang.String>
3 dup
4 ldc #2 <String "Stanford ">
6 invokespecial #12 <Method java.lang.String(java.lang.String)>
9 astore_1
10 new #8 <Class java.lang.StringBuffer>
13 dup
14 aload_1
15 invokestatic #23 <Method java.lang.String valueOf(java.lang.Object)>
18 invokespecial #13 <Method java.lang.StringBuffer(java.lang.String)>
21 ldc #1 <String "Lost!!">
23 invokevirtual #15 <Method java.lang.StringBuffer append(java.lang.String)>
26 invokevirtual #22 <Method java.lang.String toString()>
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 aStringBuffer 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 theString 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:

  1. String object at location 0
  2. StringBuffer object at location 10
  3. String object at location 26

Now, let‘s look at the bytecode generated for the example using StringBuffer:

0 new #8 <Class java.lang.StringBuffer>
3 dup
4 ldc #2 <String "Stanford ">
6 invokespecial #13 <Method java.lang.StringBuffer(java.lang.String)>
9 astore_1
10 aload_1
11 ldc #1 <String "Lost!!">
13 invokevirtual #15 <Method java.lang.StringBuffer append(java.lang.String)>
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 Stringconcatenation. Obviously, StringBuffers should be used in this type of operation when possible. If the functionality of the String class is desired, consider using aStringBuffer for concatenation and then performing one conversion to String.

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.

时间: 2024-10-25 18:18:58

[转载]StringBuffer versus String的相关文章

全面解释java中StringBuilder、StringBuffer、String类之间的关系

http://www.jb51.net/article/33398.htm String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且大量浪费有限的内存空间,StringBuffer是可变类,和线程安全的字符串操作类,任何对它指向的字符串的操作都不会产生新的对象,StringBuffer和StringBuilder类功能基本相似 1. String 类  String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不

ArrayList和Vector的区别?HashMap和HashTable的区别?StringBuilder、StringBuffer和String的区别?

ArrayList和Vector的区别?从两个方面 1.同步性:ArrayList是线程不安全的,是非同步的:Vector是线程安全的,是同步的.(Java中线程的同步也就满足了安全性) 2.数值增长:ArrayList每次增长为原来的50%;Vector每次增长为原来的100%; (从内部实现机制来讲,ArrayList和Vector都是使用数组(Array)来控制集合中的对象,当向集合中添加对象时,如果内部数组长度不够用时,长度会自动增长.ArrayList会增长为原来的1.5倍,Vecto

StringBuilder、StringBuffer和String三者的联系和区别(转)

StringBuilder.StringBuffer和String三者的联系和区别 1. String 类    String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且大量浪费有限的内存空间.    String a = "a"; //假设a指向地址0x0001    a = "b";//重新赋值后a指向地址0x0002,但0x0001地址中保存的"a"依旧存在,但已经不再是a所指向的,a 已

java中StringBuilder、StringBuffer、String类之间的关系

今天在CSDN的高校俱乐部里看到了"Java基础水平測试(英文)".感觉自己学了java这么久,想看下自己的java水平究竟是个什么样.測试结果就不说了,反正是慘不忍睹. 看了一下结果分析,关于StringBuilder.StringBuffer.String类的三道题所有答错.所以就查阅了一些资料记录一下. String的值是不可变的,这就导致每次对String的操作都会生成新的String对象.不仅效率低下,并且大量浪费有限的内存空间,StringBuffer是可变类.和线程安全的

Java基础知识强化44:StringBuffer类之StringBuffer和String的相互转化

1. String和StringBuffer的相互转换 思想是:A-----B的转换,我们把A转换为B,其实是为了使用B的功能:B-----A的转换,我们可能的结果是A类型,所以还要转换回来 2. 案例演示: 1 package cn.itcast_07; 2 3 /* 4 * 为什么我们要讲解类之间的转换: 5 * A -- B的转换 6 * 我们把A转换为B,其实是为了使用B的功能. 7 * B -- A的转换 8 * 我们可能要的结果是A类型,所以还得转回来. 9 * 10 * Strin

java StringBuffer,StringBuilder,String自身连接效率对比

当我们仅仅需要a+b 的时候,两个字符串链接任何方法的效率基本一样,都在0.0001毫秒内就可以完成.不过如果需要1万次,10000万次,就会发现string自身的join速度显著下降 package com.java.lang; public class StringTest { int MAX = 10000; //1万次累加 public String Buffer(){ StringBuffer sb = new StringBuffer(); for(int i = 0; i < MA

Stringbuffer 与String

StringBuffer与String主要的区别是: 1.StingBuffer在进行字符串处理时不生成新的对象,但是String的每次修改都是产生新的对象,所以在内存上StringBuffer优于String 2.StirngBuffer只能通过构造函数建立. StringBuffer stringbuffer=new StringBuffer("ab"); 注意:不能通过赋值符号赋值!! 3.String str = new String("welcome to &quo

简单的理解 StringBuffer/StringBuilder/String 的区别

StringBuffer/StringBuilder/String 的区别 这个三类之间主要的区别:运行速度,线程安全两个方面. 速度方面(快到慢): StringBuilder > StringBuffer > String String 慢的原因: String为字符串常量,不可被改变,StringBuilder和StringBuffer为字符串变量,可以被改变. 代码实例: 1 String demo="测试内容"; 2 System.out.println(demo

StringBuffer/StringBuilder/String的区别

1.在执行速度上:Stringbuilder->Stringbuffer->String 2.String是字符串常量 Stringbuffer是字符串变量 Stringbuilder是字符串变量 有可能我们会疑惑String怎么是字符串变量.看以下代码: String str = adc: str = str + "ef": System.out.println(str); 输出结果为:abcef; 在Java虚拟机中str为adc时是一个变量,当str被重新赋值为adc