形式参数问题
A:String作为参数传递
B:StringBuffer作为参数传递
形式参数:
基本参数:形式参数的改变不影响实际参数
引用参数:形式参数的改变直接影响实际参数
注意:
String作为参数传递,效果和基本参数传递是一样的
代码举例:
public class Test {
public static void main(String[] args) {
String s = "Hello";
String s2 = "World";
System.out.println(s + "---" + s2);//Hello---World
change(s,s2);
System.out.println(s + "---" + s2);//Hello---World
StringBuffer sb = new StringBuffer("Hello");
StringBuffer sb2 = new StringBuffer("World");
System.out.println(sb + "---" + sb2);//Hello---World
change(sb,sb2);
System.out.println(sb + "---" + sb2);//Hello---WorldWorld
}
public static void change(StringBuffer sb, StringBuffer sb2) {
sb = sb2;
sb2 = sb.append(sb2);
}
public static void change(String s, String s2) {
s = s2;
s2 = s + s2;
}
}