两者里面封的都是char 数组,但由于String是不可变对象,因此两个String相加时会生成一个新的String,但StringBuffer的内容可变,StringBuffer.append(String),不会产生新的对象。
如果字符串相加频繁,请使用StringBuffer代替String,StringBuffer.toString()得到所封装的char array所组成的String

解决方案 »

  1.   

    String是不可以修改的
    StringBuffer是专为字符串修改操作设的
      

  2.   

    其实简单地说就是:
        stringBuffer就是动态的字符串,可以方便的增减长度,修改内容
       而String 就是静态的字符串,是固定的
      

  3.   

    String 类型的字符串对象只能读取 不能修改
    StringBuffer 类型的字符串对象的长度和内容能够修改  不过用的较多的还是 String
      

  4.   

    有篇文章说:
      StringBuffer 比 String 快!
      

  5.   

    《Effective Java Programming Language Guide》  
    Item 33: Beware the performance of string concatenationThe string concatenation operator (+) is a convenient way to combine a few strings into one. It
    is fine for generating a single line of output or for constructing the string representation of a
    small, fixed-size object, but it does not scale. Using the string concatenation operator
    repeatedly to concatenate n strings requires time quadratic in n. It is an unfortunate
    consequence of the fact that strings are immutable (Item 13). When two strings are
    concatenated, the contents of both are copied.For example, consider the following method that constructs a string representation of a billing
    statement by repeatedly concatenating a line for each item:// Inappropriate use of string concatenation - Performs horribly!
       public String statement() {
           String s = "";
           for (int i = 0; i < numItems(); i++)
               s += lineForItem(i); // String concatenation
           return s;
       }This method performs abysmally if the number of items is large. To achieve acceptable
    performance, use a StringBuffer in place of a String to store the statement under
    construction:    public String statement() {
            StringBuffer s = new StringBuffer(numItems() * LINE_WIDTH);
            for (int i = 0; i < numItems(); i++)
                s.append(lineForItem(i));
            return s.toString();
        }The difference in performance is dramatic. If numItems returns 100 and lineForItem returns
    a constant 80-character string, the second method is ninety times faster on my machine than
    the first. Because the first method is quadratic in the number of items and the second is linear,
    the performance difference is even more dramatic for larger numbers of items. Note that the
    second method preallocates a StringBuffer large enough to hold the result. Even if it is
    detuned to use a default-sized StringBuffer, it is still forty-five times faster than the first.The moral is simple: Don't use the string concatenation operator to combine more than a few
    strings unless performance is irrelevant. Use StringBuffer's append method instead.
    Alternatively, use a character array, or process the strings one at a time instead of combining
    them.