既然说String类具有不可变性,而StringBuilder和StringBuffer具有可变性,怎么个可变法,能否具体举例说明?
String temp = "哇哈哈";
System.out.println(temp+"1234"); // 两个空间,分别是temp和temp+"1234"
StringBuilder sb = new StringBuilder("哇哈哈");
System.out.println(sb.append("1234")); // 一个空间
有点理解了,一个相当于在原来的位置旁边接着写,另一个相当于把原来的擦掉重写.
delete(int start, int end)
Removes the characters in a substring of this sequence.
replace(int start, int end, String str)
Replaces the characters in a substring of this sequence with characters in the specified String.