Java中String和StringBuffer有什么区别?

发布于 2024-08-25 00:14:53 字数 56 浏览 7 评论 0原文

Java中String和StringBuffer有什么区别?

字符串有最大大小吗?

What is the difference between String and StringBuffer in Java?

Is there a maximum size for String?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

站稳脚跟 2024-09-01 00:14:53

String 用于操作不可更改的字符串(只读且不可变)。

StringBuffer 用于表示可以修改的字符。

从性能角度来看,StringBuffer 在执行串联时速度更快。这是因为,当您连接 String 时,每次都会(在内部)创建一个新对象,因为 String 是不可变的。

您还可以使用StringBuilder,它与StringBuffer类似,只是它不同步。其中任何一个的最大大小为 Integer.MAX_VALUE (231 - 1 = 2,147,483,647) 或最大堆大小除以 2(请参阅 Java 字符串可以有多少个字符?)。
更多信息请参见此处

String is used to manipulate character strings that cannot be changed (read-only and immutable).

StringBuffer is used to represent characters that can be modified.

Performance wise, StringBuffer is faster when performing concatenations. This is because when you concatenate a String, you are creating a new object (internally) every time since String is immutable.

You can also use StringBuilder which is similar to StringBuffer except it is not synchronized. The maximum size for either of these is Integer.MAX_VALUE (231 - 1 = 2,147,483,647) or maximum heap size divided by 2 (see How many characters can a Java String have?).
More information here.

巾帼英雄 2024-09-01 00:14:53

String 是不可变的,即当它被创建时,它永远不会改变。

当您需要逐段构造字符串而不需要构造大量小 String 的性能开销时,可以使用 StringBuffer (或其非同步表亲 StringBuilder)一路走来。

两者的最大长度都是 Integer.MAX_VALUE,因为它们在内部存储为数组,而 Java 数组只有一个 int 作为其长度伪字段。

对于多重串联,StringStringBuffer 之间的性能改进非常显着。如果运行以下测试代码,您将看到差异。在我装有 Java 6 的老式笔记本电脑上,我得到以下结果:

Concat with String took: 1781ms
Concat with StringBuffer took: 0ms
public class Concat
{
    public static String concatWithString()
    {
        String t = "Cat";
        for (int i=0; i<10000; i++)
        {
            t = t + "Dog";
        }
        return t;
    }
    public static String concatWithStringBuffer()
    {
        StringBuffer sb = new StringBuffer("Cat");
        for (int i=0; i<10000; i++)
        {
            sb.append("Dog");
        }
        return sb.toString();
    }
    public static void main(String[] args)
    {
        long start = System.currentTimeMillis();
        concatWithString();
        System.out.println("Concat with String took: " + (System.currentTimeMillis() - start) + "ms");
        start = System.currentTimeMillis();
        concatWithStringBuffer();
        System.out.println("Concat with StringBuffer took: " + (System.currentTimeMillis() - start) + "ms");
    }
}

A String is immutable, i.e. when it's created, it can never change.

A StringBuffer (or its non-synchronized cousin StringBuilder) is used when you need to construct a string piece by piece without the performance overhead of constructing lots of little Strings along the way.

The maximum length for both is Integer.MAX_VALUE, because they are stored internally as arrays, and Java arrays only have an int for their length pseudo-field.

The performance improvement between Strings and StringBuffers for multiple concatenation is quite significant. If you run the following test code, you will see the difference. On my ancient laptop with Java 6, I get these results:

Concat with String took: 1781ms
Concat with StringBuffer took: 0ms
public class Concat
{
    public static String concatWithString()
    {
        String t = "Cat";
        for (int i=0; i<10000; i++)
        {
            t = t + "Dog";
        }
        return t;
    }
    public static String concatWithStringBuffer()
    {
        StringBuffer sb = new StringBuffer("Cat");
        for (int i=0; i<10000; i++)
        {
            sb.append("Dog");
        }
        return sb.toString();
    }
    public static void main(String[] args)
    {
        long start = System.currentTimeMillis();
        concatWithString();
        System.out.println("Concat with String took: " + (System.currentTimeMillis() - start) + "ms");
        start = System.currentTimeMillis();
        concatWithStringBuffer();
        System.out.println("Concat with StringBuffer took: " + (System.currentTimeMillis() - start) + "ms");
    }
}
安静被遗忘 2024-09-01 00:14:53
String                                          StringBuffer

Immutable                                       Mutable
String s=new String("karthik");                StringBuffer sb=new StringBuffer("karthik")
s.concat("reddy");                             sb.append("reddy");
System.out.println(s);                         System.out.println(sb);
O/P:karthik                                    O/P:karthikreddy

--->once we created a String object            ---->once we created a StringBuffer object
we can't perform any changes in the existing  we can perform any changes in the existing
object.If we are trying to perform any        object.It is nothing but mutablity of 
changes with those changes a new object       of a StrongBuffer object
will be created.It is nothing but Immutability
of a String object

Use String--->If you require immutabilty
Use StringBuffer---->If you require mutable + threadsafety
Use StringBuilder--->If you require mutable + with out threadsafety

String s=new String("karthik");
--->here 2 objects will be created one is heap and the other is in stringconstantpool(scp) and s is always pointing to heap object

String s="karthik"; 
--->In this case only one object will be created in scp and s is always pointing to that object only
String                                          StringBuffer

Immutable                                       Mutable
String s=new String("karthik");                StringBuffer sb=new StringBuffer("karthik")
s.concat("reddy");                             sb.append("reddy");
System.out.println(s);                         System.out.println(sb);
O/P:karthik                                    O/P:karthikreddy

--->once we created a String object            ---->once we created a StringBuffer object
we can't perform any changes in the existing  we can perform any changes in the existing
object.If we are trying to perform any        object.It is nothing but mutablity of 
changes with those changes a new object       of a StrongBuffer object
will be created.It is nothing but Immutability
of a String object

Use String--->If you require immutabilty
Use StringBuffer---->If you require mutable + threadsafety
Use StringBuilder--->If you require mutable + with out threadsafety

String s=new String("karthik");
--->here 2 objects will be created one is heap and the other is in stringconstantpool(scp) and s is always pointing to heap object

String s="karthik"; 
--->In this case only one object will be created in scp and s is always pointing to that object only
面如桃花 2024-09-01 00:14:53

String 是一个不可变的类。这意味着一旦实例化字符串的实例,如下所示:

String str1 = "hello";

内存中的对象无法更改。相反,您必须创建一个新实例,复制旧字符串并附加其他内容,如本例所示:

String str1 = "hello";
str1 = str1 + " world!";

真正发生的情况是我们没有更新现有的 str1 对象...我们正在重新分配新内存,复制“hello”数据并附加“world!”到最后,然后将 str1 引用设置为指向这个新内存。因此,它实际上看起来更像是这样的:

String str1 = "hello";
String str2 = str1 + " world!";
str1 = str2;

因此,如果重复执行,尤其是递归执行,这种“复制+粘贴并在内存中移动内容”过程可能会非常昂贵。

当您处于必须一遍又一遍地做事情的情况时,请使用 StringBuilder。它是可变的,并且可以将字符串附加到当前字符串的末尾,因为它由一个[增长数组]返回(如果这是实际的数据结构,则不是 100%,可能是一个列表)。

String is an immutable class. This means that once you instantiate an instance of a string like so:

String str1 = "hello";

The object in memory cannot be altered. Instead you will have to create a new instance, copy the old String and append whatever else as in this example:

String str1 = "hello";
str1 = str1 + " world!";

What is really happening hear is that we are NOT updating the existing str1 object... we are reallocating new memory all together, copying the "hello" data and appending " world!" to the end, then settings the str1 reference to point to this new memory. So it really looks more like this under the hood:

String str1 = "hello";
String str2 = str1 + " world!";
str1 = str2;

So it follows that this "copy + paste and move stuff around in memory" process can be very expensive if done repitively especially recursively.

When you are in that situation of having to do things over and over utilize StringBuilder. It is mutable and can append strings to the end of the current one because it's back by an [growing array] (not 100% if that is the actual data structure, could be a list).

温柔戏命师 2024-09-01 00:14:53

来自 API:

线程安全、可变的字符序列。字符串缓冲区类似于字符串,但可以修改。在任何时间点,它都包含一些特定的字符序列,但序列的长度和内容可以通过某些方法调用来更改。

From the API:

A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

会发光的星星闪亮亮i 2024-09-01 00:14:53

StringBuffer 用于从多个字符串创建单个字符串,例如,当您想要在循环中附加字符串的一部分时。

当只有一个线程访问 StringBuffer 时,您应该使用 StringBuilder 而不是 StringBuffer,因为 StringBuilder 不同步,因此速度更快。

AFAIK 作为一种语言,Java 中的字符串大小没有上限,但 JVM 可能有上限。

A StringBuffer is used to create a single string from many strings, e.g. when you want to append parts of a String in a loop.

You should use a StringBuilder instead of a StringBuffer when you have only a single Thread accessing the StringBuffer, since the StringBuilder is not synchronized and thus faster.

AFAIK there is no upper limit for String size in Java as a language, but the JVMs probably have an upper limit.

卖梦商人 2024-09-01 00:14:53

我找到了 Reggie Hutcherso 比较 String 与 StringBuffer 性能的有趣答案
来源http:// www.javaworld.com/javaworld/jw-03-2000/jw-0324-javaperf.html

Java提供了StringBuffer和String类,String类用于操作不能改变的字符串。简单地说,String 类型的对象是只读且不可变的。 StringBuffer类用于表示可以修改的字符。

这两个类之间的显着性能差异是,在执行简单连接时,StringBuffer 比 String 更快。在字符串操作代码中,字符串通常被连接起来。使用 String 类,连接通常按如下方式执行:

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

如果要使用 StringBuffer 执行相同的连接,则需要如下所示的代码:

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

开发人员通常认为上面的第一个示例更有效,因为他们认为第二个示例更有效。使用append方法进行连接的示例比第一个示例(使用+运算符连接两个String对象)成本更高。

+ 运算符看似无辜,但生成的代码却产生了一些惊喜。使用 StringBuffer 进行串联实际上可以生成比使用 String 快得多的代码。为了找出为什么会出现这种情况,我们必须检查两个示例生成的字节码。使用 String 的示例的字节码如下所示:

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

对于第一行代码,执行位置 0 到 9 的字节码,即:

 String str = new String("Stanford ");

然后,执行连接的位置 10 到 29 的字节码:

 str += "Lost!!";

这里事情变得有趣了。为串联生成的字节码创建一个 StringBuffer 对象,然后调用其追加方法:临时 StringBuffer 对象在位置 10 创建,并在位置 23 调用其追加方法。由于 String 类是不可变的,因此必须使用 StringBuffer级联。

对 StringBuffer 对象执行串联后,必须将其转换回 String。这是通过调用位置 26 处的 toString 方法来完成的。该方法从临时 StringBuffer 对象创建一个新的 String 对象。创建这个临时 StringBuffer 对象以及随后将其转换回 String 对象的成本非常高。

总之,上面的两行代码创建了三个对象:

  1. 位置 0 的 String 对象
  2. 位置 10 的 StringBuffer 对象
  3. 位置 26 的 String 对象

为示例生成的字节码:

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

现在,让我们看看使用StringBuffer 对于第一行代码,执行位置 0 到 9 的字节码:

 StringBuffer str = new StringBuffer("Stanford ");

然后执行位置 10 到 16 的字节码以进行串联:

 str.append("Lost!!");

请注意,与第一个示例中的情况一样,此代码调用 StringBuffer 的 append 方法目的。然而,与第一个示例不同的是,不需要创建临时 StringBuffer 然后将其转换为 String 对象。此代码仅在位置 0 处创建一个对象,即 StringBuffer。

总之,StringBuffer 串联比 String 串联要快得多。显然,如果可能的话,应该在此类操作中使用 StringBuffer。如果需要 String 类的功能,请考虑使用 StringBuffer 进行串联,然后执行一次到 String 的转换。

I found interest answer for compare performance String vs StringBuffer by Reggie Hutcherso
Source: http://www.javaworld.com/javaworld/jw-03-2000/jw-0324-javaperf.html

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 <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 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:

  1. A String object at location 0
  2. A StringBuffer object at location 10
  3. A 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 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.

撩心不撩汉 2024-09-01 00:14:53

通过在任何追加操作后打印 String/StringBuffer 对象的哈希码也证明,String 对象每次都会使用新值在内部重新创建,而不是使用相同的 String 对象。

public class MutableImmutable {

/**
 * @param args
 */
public static void main(String[] args) {
    System.out.println("String is immutable");
    String s = "test";
    System.out.println(s+"::"+s.hashCode());
    for (int i = 0; i < 10; i++) {
        s += "tre";
        System.out.println(s+"::"+s.hashCode());
    }

    System.out.println("String Buffer is mutable");

    StringBuffer strBuf = new StringBuffer("test");
    System.out.println(strBuf+"::"+strBuf.hashCode());
    for (int i = 0; i < 10; i++) {
        strBuf.append("tre");
        System.out.println(strBuf+"::"+strBuf.hashCode());
    }

 }

}

输出:
它打印对象值及其哈希码

    String is immutable
    test::3556498
    testtre::-1422435371
    testtretre::-1624680014
    testtretretre::-855723339
    testtretretretre::2071992018
    testtretretretretre::-555654763
    testtretretretretretre::-706970638
    testtretretretretretretre::1157458037
    testtretretretretretretretre::1835043090
    testtretretretretretretretretre::1425065813
    testtretretretretretretretretretre::-1615970766
    String Buffer is mutable
    test::28117098
    testtre::28117098
    testtretre::28117098
    testtretretre::28117098
    testtretretretre::28117098
    testtretretretretre::28117098
    testtretretretretretre::28117098
    testtretretretretretretre::28117098
    testtretretretretretretretre::28117098
    testtretretretretretretretretre::28117098
    testtretretretretretretretretretre::28117098

By printing the hashcode of the String/StringBuffer object after any append operation also prove, String object is getting recreated internally every time with new values rather than using the same String object.

public class MutableImmutable {

/**
 * @param args
 */
public static void main(String[] args) {
    System.out.println("String is immutable");
    String s = "test";
    System.out.println(s+"::"+s.hashCode());
    for (int i = 0; i < 10; i++) {
        s += "tre";
        System.out.println(s+"::"+s.hashCode());
    }

    System.out.println("String Buffer is mutable");

    StringBuffer strBuf = new StringBuffer("test");
    System.out.println(strBuf+"::"+strBuf.hashCode());
    for (int i = 0; i < 10; i++) {
        strBuf.append("tre");
        System.out.println(strBuf+"::"+strBuf.hashCode());
    }

 }

}

Output:
It prints object value along with its hashcode

    String is immutable
    test::3556498
    testtre::-1422435371
    testtretre::-1624680014
    testtretretre::-855723339
    testtretretretre::2071992018
    testtretretretretre::-555654763
    testtretretretretretre::-706970638
    testtretretretretretretre::1157458037
    testtretretretretretretretre::1835043090
    testtretretretretretretretretre::1425065813
    testtretretretretretretretretretre::-1615970766
    String Buffer is mutable
    test::28117098
    testtre::28117098
    testtretre::28117098
    testtretretre::28117098
    testtretretretre::28117098
    testtretretretretre::28117098
    testtretretretretretre::28117098
    testtretretretretretretre::28117098
    testtretretretretretretretre::28117098
    testtretretretretretretretretre::28117098
    testtretretretretretretretretretre::28117098
川水往事 2024-09-01 00:14:53

StringBuffer 或其更年轻、更快的兄弟 StringBuilder 是首选,每当您要进行大量字符串连接(类似

string += newString;

或等效)

string = string + newString;

时,因为上述构造每次都会隐式创建 new 字符串这将是一个巨大的性能和下降。 StringBuffer / StringBuilder 的底层最好与可动态扩展的 List 进行比较。

A StringBuffer or its younger and faster brother StringBuilder is preferred whenever you're going do to a lot of string concatenations in flavor of

string += newString;

or equivalently

string = string + newString;

because the above constructs implicitly creates new string everytime which will be a huge performance and drop. A StringBuffer / StringBuilder is under the hoods best to be compared with a dynamically expansible List<Character>.

纵情客 2024-09-01 00:14:53

String 是一个不可变的字符数组。

StringBuffer 是一个可变字符数组。完成变异后通常会转换回 String

由于两者都是数组,因此两者的最大大小都等于整数的最大大小,即 2^31-1(请参见 JavaDoc,另请查看 StringStringBuffer 的 JavaDoc code>)。这是因为数组的 .length 参数是原始 int。 (请参阅数组)。

A String is an immutable character array.

A StringBuffer is a mutable character array. Often converted back to String when done mutating.

Since both are an array, the maximum size for both is equal to the maximum size of an integer, which is 2^31-1 (see JavaDoc, also check out the JavaDoc for both String and StringBuffer).This is because the .length argument of an array is a primitive int. (See Arrays).

浅紫色的梦幻 2024-09-01 00:14:53

字符串是不可变的,这意味着当您对字符串执行操作时,您实际上是在创建一个全新的字符串。

StringBuffer 是可变的,您可以附加到并将其长度重置为 0。

实际上,编译器似乎在字符串连接期间使用 StringBuffer 出于性能原因

String is immutable, meaning that when you perform an operation on a String you are really creating a whole new String.

StringBuffer is mutable, and you can append to it as well as reset its length to 0.

In practice, the compiler seems to use StringBuffer during String concatenation for performance reasons.

罪歌 2024-09-01 00:14:53
String is immutable. 

为什么?请查看此处

StringBuffer is not. It is thread safe. 

进一步的问题,例如何时使用哪些概念和其他概念,可以通过以下方式找出

希望这有帮助。

String is immutable. 

Why? Check here.

StringBuffer is not. It is thread safe. 

Further questions like when to use which and other concepts can be figured out following this.

Hope this helps.

空宴 2024-09-01 00:14:53

虽然我知道这不是主要的区别因素,但今天我注意到 StringBuffer(和 StringBuilder)提供了一些 String 没有的有趣方法。

  • 反向()
  • setCharAt()

While I understand that this is not a major differentiating factor, I noticed today that StringBuffer(and StringBuilder) provides some interesting methods that String doesn't.

  • reverse()
  • setCharAt()
就此别过 2024-09-01 00:14:53

区别在于

  1. 仅在String类中重载了+运算符。我们可以使用+运算符连接两个String对象,但在StringBuffer的情况下我们不能。
  2. String类重写了Object类的toString()、equals()、hashCode(),但StringBuffer仅重写了toString() .

    String s1 = new String("abc");
    字符串 s2 = new String("abc");
    System.out.println(s1.equals(s2)); // 输出真
    
    StringBuffer sb1 = new StringBuffer("abc");
    StringBuffer sb2 = new StringBuffer("abc");
    System.out.println(sb1.equals(sb2)); // 输出假
    
  3. String 类既可序列化可比较,但 StringBuffer 只能可序列化< /strong>.

    设置 set = new TreeSet();
    设置.add(sb1);
    设置.add(sb2);
    System.out.println(set); // 给出 ClassCastException 因为没有比较机制
    

  4. 我们可以使用或不使用 new 运算符创建 String 对象,但 StringBuffer 对象只能使用 new 运算符创建。

  5. String 是不可变的,但 StringBuffer 是可变的。
  6. StringBuffer 是同步的,而 String 不是。
  7. StringBuffer 有一个内置的 reverse() 方法,但 String 没有它。

The differences are

  1. Only in String class + operator is overloaded. We can concat two String object using + operator, but in the case of StringBuffer we can't.
  2. String class is overriding toString(), equals(), hashCode() of Object class, but StringBuffer only overrides toString().

    String s1 = new String("abc");
    String s2 = new String("abc");
    System.out.println(s1.equals(s2));  // output true
    
    StringBuffer sb1 = new StringBuffer("abc");
    StringBuffer sb2 = new StringBuffer("abc");
    System.out.println(sb1.equals(sb2));  // output false
    
  3. String class is both Serializable as well as Comparable, but StringBuffer is only Serializable.

    Set<StringBuffer> set = new TreeSet<StringBuffer>();
    set.add(sb1);
    set.add(sb2);
    System.out.println(set);  // gives ClassCastException because there is no Comparison mechanism
    
  4. We can create a String object with and without new operator, but StringBuffer object can only be created using new operator.

  5. String is immutable but StringBuffer is mutable.
  6. StringBuffer is synchronized, whereas String ain't.
  7. StringBuffer is having an in-built reverse() method, but String dosen't have it.
半﹌身腐败 2024-09-01 00:14:53

性能方面 StringBuffer 比 String 好得多;因为每当您在 String 对象上应用串联时,就会在每个串联上创建新的 String 对象。

主要规则:字符串是不可变的(不可修改)和 StringBuffer 是可变的(可修改)

这是您获得性能差异的编程实验

public class Test {

  public static int LOOP_ITERATION= 100000;

  public static void stringTest(){
    long startTime = System.currentTimeMillis();
    String string = "This";
    for(int i=0;i<LOOP_ITERATION;i++){
        string = string+"Yasir";
    }

    long endTime = System.currentTimeMillis();
    System.out.println(endTime - startTime);    
  }

  public static void stringBufferTest(){
    long startTime = System.currentTimeMillis();
    StringBuffer stringBuffer = new StringBuffer("This");
    for(int i=0;i<LOOP_ITERATION;i++){
        stringBuffer.append("Yasir");
    }

    long endTime = System.currentTimeMillis();
    System.out.println(endTime - startTime);
  }

  public static void main(String []args){
    stringTest()
    stringBufferTest(); 
  }
 }

字符串的输出在我的机器中
14800

StringBuffer 的输出在我的机器中
14

Performance wise StringBuffer is much better than String ; because whenever you apply concatenation on String Object then new String object are created on each concatenation.

Principal Rule : String are immutable(Non Modifiable) and StringBuffer are mutable(Modifiable)

Here is the programmatic experiment where you get the performance difference

public class Test {

  public static int LOOP_ITERATION= 100000;

  public static void stringTest(){
    long startTime = System.currentTimeMillis();
    String string = "This";
    for(int i=0;i<LOOP_ITERATION;i++){
        string = string+"Yasir";
    }

    long endTime = System.currentTimeMillis();
    System.out.println(endTime - startTime);    
  }

  public static void stringBufferTest(){
    long startTime = System.currentTimeMillis();
    StringBuffer stringBuffer = new StringBuffer("This");
    for(int i=0;i<LOOP_ITERATION;i++){
        stringBuffer.append("Yasir");
    }

    long endTime = System.currentTimeMillis();
    System.out.println(endTime - startTime);
  }

  public static void main(String []args){
    stringTest()
    stringBufferTest(); 
  }
 }

Output of String are in my machine
14800

Output of StringBuffer are in my machine
14

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文