如下代码:
public class StringBufferTest
{
public static void main(String args[])
{
final StringBuffer sb = new StringBuffer("ABC");
sb.append("DEF");
System.out.println(sb);
// final String str = "ABC";
// str = "DEF";
// System.out.println(str);
StringBuffer sb1 = new StringBuffer("A");
StringBuffer sb2 = new StringBuffer("B");
System.out.println(sb1 + "," + sb2);
test(sb1, sb2);
System.out.println(sb1 + "," + sb2);
Integer i1 = new Integer(2);
Integer i2 = new Integer(3);
System.out.println(i1 + "," + i2);
test2(i1, i2);
System.out.println(i1 + "," + i2);
}
private static void test(StringBuffer x, StringBuffer y)
{
x = x.append(y).append(x);
y = x;
}
private static void test2(Integer x, Integer y)
{
x = x + y;
y = x + 3;
}
}
输出为:
ABCDEF
A,B
ABAB,B
2,3
2,3