本质上的区别是:String是不可变类(immutable),而StringBuffer是可变类。
对于String而言,创建后的值不会被修改,并且相同的字符串对象是相同的地址引用。
关于速度问题,看具体的应用场合不同来选择。使用上各自都有各自的作用。

解决方案 »

  1.   

    The Java platform provides two classes, String and StringBuffer, that store and manipulate strings-character data consisting of more than one character. The String class provides for strings whose value will not change. For example, if you write a method that requires string data and the method is not going to modify the string in any way, pass a String object into the method. The StringBuffer class provides for strings that will be modified; you use string buffers when you know that the value of the character data will change. You typically use string buffers for constructing character data dynamically: for example, when reading text data from a file. Because strings are constants, they are more efficient to use than are string buffers and can be shared. So it's important to use strings when you can. 
    Following is a sample program called StringsDemo, which reverses the characters of a string. This program uses both a string and a string buffer. public class StringsDemo {
        public static void main(String[] args) {
            String palindrome = "Dot saw I was Tod";
            int len = palindrome.length();
            StringBuffer dest = new StringBuffer(len);        for (int i = (len - 1); i >= 0; i--) {
                dest.append(palindrome.charAt(i));
            }
            System.out.println(dest.toString());
        }
    }The output from this program is: 
    doT saw I was toDIn addition to highlighting the differences between strings and string buffers, this section discusses several features of the String and StringBuffer classes: creating strings and string buffers, using accessor methods to get information about a string or string buffer, and modifying a string buffer.