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

62

Java中String和StringBuffer有什么区别?

String是否有最大长度限制?


14
将来,您可以查看Javadocs以获取此类问题的答案。Java 6 javadocs: http://java.sun.com/javase/6/docs/api/ String: http://java.sun.com/javase/6/docs/api/java/lang/String.html StringBuffer: http://java.sun.com/javase/6/docs/api/java/lang/StringBuffer.html - Seth
15个回答

1

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

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

由于它们都是数组,所以两者的最大大小等于整数的最大大小,即 2^31-1(参见 JavaDoc,也请查看 StringStringBuffer 的 JavaDoc)。这是因为数组的 .length 参数是原始的 int。(请参见 Arrays)。


数组的长度是一个有符号32位整数int。Pindatjuh引用的值是Integer.MAX_VALUE,这是正确的。但是类本身并没有强制规定最大长度,因此从某种意义上说,Pindatjuh是不正确的,因为他在看实现的幕后。没有理由不能将String实现为char [] [],然后限制为Long.MAX_VALUE等等。 - President James K. Polk
@Thomas Lötzer 添加了源代码。 - Pindatjuh
在JLS中哪里说String必须作为单个数组实现?另外:由于字符串文字被放入常量池中,因此它们不能超过Integer.MAX_VALUE个字符长。它们只能是2^16个字符。如何确保其他字符串没有这样的限制? - Thomas Lötzer
1
@GregS:然而,String类有一个toCharArray()方法,如果你有一个超过Integer.MAX_VALUE字符的字符串,这个方法将无法实现。 - Simon Nickerson
@Simon:我同意,该方法和charAt方法暗示了一个字符串被限制在Integer.MAX_VALUE个字符以内。 - President James K. Polk
显示剩余3条评论

1

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

  • reverse()
  • setCharAt()

1

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

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

在实践中,编译器似乎在字符串连接出于性能原因时使用StringBuffer。


1

差异在于

  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.

0

从性能上看,StringBuffer比String好得多;因为每次在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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接