The class is one of three core string classes in the Java programming language. Most of the time, the class is used, however, the StringBuffer class is a mutable object while String is immutable. That means the contents of StringBuffer objects can be modified while similar methods in the String class that appear to modify the contents of the string actually return a new String object.
| Property | Value |
| p:abstract
| - The class is one of three core string classes in the Java programming language. Most of the time, the class is used, however, the StringBuffer class is a mutable object while String is immutable. That means the contents of StringBuffer objects can be modified while similar methods in the String class that appear to modify the contents of the string actually return a new String object. It is more efficient to use a StringBuffer instead of operations that result in the creation of many intermediate String objects. The class, introduced in J2SE 5.0, differs from StringBuffer in that it is unsynchronized. When only a single thread at a time will access the object, using a StringBuilder is more efficient than using a StringBuffer.
StringBuffer and StringBuilder are included in the package.
The following example code uses StringBuffer instead of String for performance improvement.
StringBuffer sbuf = new StringBuffer(300);
for (int i = 0; i
Moreover, the Java compiler (e.g., javac) usually uses StringBuilder (or StringBuffer) to concatenate strings and objects joined by the additive operator. For example, one can expect that
String s = "a + b = " + a + b;
is translated to
StringBuffer sbuf = new StringBuffer(32);
sbuf.append("a + b = ").append(a).append(b);
String s = sbuf.toString;
In the above code, a and b can be almost anything from primitive values to objects. (en)
- クラスはJavaにある二つの中核となる文字列クラスのひとつである。多くの場合、クラスが使われるが、StringBuffer クラスはミュータブルオブジェクトであり、一方 String クラスは イミュータブルである。 つまり StringBuffer オブジェクトは更新されうるが、String クラスでは似たようなメソッドが新たにStringオブジェクトを生成しメソッドの戻り値としてreturnされた文字列の内容が変更されることで実現される。多くのString オブジェクトを生成しては捨てていく処理の替わりにStringBufferを使った方がより効率的である。
クラスは、同期を考慮しないことがStringBufferとは異なる点であり、これはJ2SE 5.0で導入された。オブジェクトにアクセスするシングルスレッドのときだけ、StringBuilderはStringBufferよりもより効率的に使われる。
StringBuffer と StringBuilderは パッケージに含まれている。
パフォーマンス改善としてStringの替わりにStringBufferを使ったコード例を以下に示す。
StringBuffer sbuf = new StringBuffer(300);
for (int i = 0; i StringBuilder (または StringBuffer)を使用する。例として、下のコード
String s = "a + b = " + a + b;
は以下のように解釈される。
StringBuffer sbuf = new StringBuffer(32);
sbuf.append ("a + b = ");
sbuf.append(a);
sbuf.append(b);
String s = sbuf.toString;
上のコードでは、a と b はプリミティブ変数でもオブジェクトでもよい。 (ja)
|
| p:date
| - 2008-05-01 00:00:00.000000 (xsd:date)
|
| p:hasPhotoCollection
| |
| p:javadocSeProperty
| - String (en)
- StringBuffer (en)
- StringBuilder (en)
- java/lang (en)
|
| p:package
| |
| p:wikiPageUsesTemplate
| |
| p:wikipage-ja
| |
| rdfs:comment
| - The class is one of three core string classes in the Java programming language. Most of the time, the class is used, however, the StringBuffer class is a mutable object while String is immutable. That means the contents of StringBuffer objects can be modified while similar methods in the String class that appear to modify the contents of the string actually return a new String object. (en)
- クラスはJavaにある二つの中核となる文字列クラスのひとつである。多くの場合、クラスが使われるが、StringBuffer クラスはミュータブルオブジェクトであり、一方 String クラスは イミュータブルである。 つまり StringBuffer オブジェクトは更新されうるが、String クラスでは似たようなメソッドが新たにStringオブジェクトを生成しメソッドの戻り値としてreturnされた文字列の内容が変更されることで実現される。多くのString オブジェクトを生成しては捨てていく処理の替わりにStringBufferを使った方がより効率的である。 (ja)
|
| rdfs:label
| - StringBuffer and StringBuilder (en)
- StringBufferとStringBuilder (ja)
|
| skos:subject
| |
| foaf:page
| |
| p:redirect
| |