Java中的String和StringBuffer有什么区别?

Pra*_*een 63 java string stringbuilder stringbuffer

Java中的String和StringBuffer有什么区别?

String的最大大小是多少?

Viv*_*ath 75

String 用于处理无法更改的字符串(只读和不可变).

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

性能方面,StringBuffer在执行连接时速度更快.这是因为当你连接a时String,你每次都在创建一个新对象(内部),因为它String是不可变的.

您也可以使用StringBuilder类似的,StringBuffer但不同步.其中任何一个的最大大小是Integer.MAX_VALUE(2 31 - 1 = 2,147,483,647)或最大堆大小除以2(请参阅Java String可以有多少个字符?).更多信息在这里.

  • @Thomas http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#MAX_VALUE如果你把2 ^ 31 - 1放入谷歌你得到2,137,483,647 (2认同)

Sim*_*son 34

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

当你需要逐个构造一个字符串而没有沿着构建大量小s 的性能开销时,使用A StringBuffer(或其非同步的表兄弟StringBuilder)String.

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

Strings和StringBuffers 之间针对多个级联的性能改进是非常重要的.如果您运行以下测试代码,您将看到差异.在我使用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");
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 24

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
Run Code Online (Sandbox Code Playgroud)


Fei*_*ngo 10

String是一个不可变类.这意味着一旦你像这样实例化一个字符串的实例:

String str1 = "hello";
Run Code Online (Sandbox Code Playgroud)

内存中的对象无法更改.相反,您将不得不创建一个新实例,复制旧String并附加其他任何内容,如下例所示:

String str1 = "hello";
str1 = str1 + " world!";
Run Code Online (Sandbox Code Playgroud)

真正发生的事情是我们没有更新现有的str1对象...我们正在重新分配新内存,复制"hello"数据并附加"world!" 到最后,然后将str1引用设置为指向此新内存.因此它真的看起来更像是在引擎盖下:

String str1 = "hello";
String str2 = str1 + " world!";
str1 = str2;
Run Code Online (Sandbox Code Playgroud)

因此,如果特别是递归地进行复制,那么"复制+粘贴并在内存中移动内容"过程可能非常昂贵.

当你处于不得不反复使用StringBuilder的情况时.它是可变的并且可以将字符串附加到当前字符串的末尾,因为它是由[增长的数组]返回的(如果这是实际的数据结构,则不是100%,可以是列表).