public class Exercise8 { public static void main(String[] args) { StringBuffer a = new StringBuffer("A"); StringBuffer b = new StringBuffer("B"); operate(a, b); System.out.println(a + "," + b); } public static void operate(StringBuffer x, StringBuffer y) { x.append(y); y = x; y.append('x'); }}
答案
ABx,B
值传递、不可变对象
class TEXT { public int num; public String str; public TEXT(int num, String str) { this.num = num; this.str = str; }}public class Exercise2 { // 都是值传递,区别在于这个值代表的是数据还是指针 public static void f1(TEXT tIn, int intIn, Integer integerIn, String strIn){ tIn.num =200; tIn.str = "bcd"; // 形参和实参指向的是同一个 TEXT 的对象,修改了属性,就相当于修改实参对象的属性 intIn = 200; // 数据 integerIn = 200; strIn = "bcd"; // 包装类和 String,都是不可变对象,一旦修改都是新对象,不影响原对象 } public static void main(String[] args) { TEXT tIn = new TEXT(100, "abc"); int intIn = 100; Integer integerIn = 100; String strIn = "abc"; f1(tIn, intIn, integerIn, strIn); System.out.println(tIn.num + tIn.str + intIn + integerIn + strIn); }}
答案
200bcd100100abc
值传递、“遮蔽”
public class Exercise5 { int a; int b; String str; public void f() { a = 0; b = 0; str = "hello"; int[] c = {0}; g(b, c, str); System.out.println(a + " " + b + " " + c[0] + "," + str); } public void g(int b, int[] c, String s) { a = 1; b = 1; c[0] = 1; s = "world"; } public static void main(String[] args) { Exercise5 t = new Exercise5(); t.f(); }}
答案
1 0 1,hello
String 和 StringBuffer 1
public class Exercise3 { public static void stringReplace(String text) { text = text.replace('j', 'i'); } public static void bufferReplace(StringBuffer text) { text.append("C"); text = new StringBuffer("Hello"); text.append("World!"); } public static void main(String[] args) { String textString = new String("java"); StringBuffer textBuffer = new StringBuffer("java"); stringReplace(textString); bufferReplace(textBuffer); System.out.println(textString); System.out.println(textBuffer); }}
答案
javajavaC
String 和 StringBuffer 2
public class Exercise6 { private static void change(String s, StringBuffer sb) { s = "aaaa"; // 字符串对象是不可变,一旦修改,就是新对象 sb.setLength(0); // 先把 sb 里面的内容给清空了 sb.append("aaaa"); // 再拼接 aaaa } public static void main(String[] args) { String s = "bbbb"; StringBuffer sb = new StringBuffer("bbbb"); change(s, sb); System.out.println(s + sb); }}
答案
bbbbaaaa
String 和 char[] 1
public class Exercise4 { public static void main(String[] args) { String str = new String("world"); char[] ch = new char[]{'h', 'e', 'l', 'l', 'o'}; change(str, ch); System.out.println(str); System.out.println(String.valueOf(ch)); } public static void change(String str, char[] arr) { str = "change"; arr[0] = 'a'; arr[1] = 'b'; arr[2] = 'c'; arr[3] = 'd'; arr[4] = 'e'; }}
答案
worldabcde
String 和 char[] 2
public class Exercise7 { String str = new String("good"); char[] ch = {'a', 'b', 'c'}; public static void main(String[] args) { Exercise7 ex = new Exercise7(); ex.change(ex.str, ex.ch); System.out.print(ex.str + " and ");//good + and + gbc System.out.print(ex.ch); } public void change(String str, char[] ch) { str = "test ok"; ch[0] = 'g'; }}