方法的参数是引用的复制
测试案例一
/**
* @author 欢迎加入Java技术交流群:646766275
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");
operate(a,b);
System.out.println(a + "," + b);
}
static void operate(StringBuffer x, StringBuffer y) {
x.append(y);
y = x;
//System.out.println("x=" + x + ",y=" + y);
}
}
main方法输出结果:
AB,B
operate方法输出结果:
x=AB,y=AB
测试案例二
/**
* @author 欢迎加入Java技术交流群:646766275
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String a = "A";
String b = "B";
operate(a, b);
System.out.println(a + "," + b);
}
static void operate(String x, String y) {
x += y;
y = x;
//System.out.println("x=" + x + ",y=" + y);
}
}
main方法输出结果:
A,B
operate方法输出结果:
x=AB,y=AB
测试案例三
/**
* @author 欢迎加入Java技术交流群:646766275
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer s = new StringBuffer("Java");
String c = new String("Java");
System.out.println(s + " " + c);
operate(s, c);
System.out.println(s + " " + c);
}
public static void operate(StringBuffer s, String c) {
s.append("C");
c = s.toString();
System.out.println(s + " " + c);
}
}
main方法输出结果:
Java Java
JavaC Java
operate方法输出结果:
JavaC JavaC
测试案例四
/**
* @author 欢迎加入Java技术交流群:646766275
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer i = new Integer(0);
operate(i);
System.out.println(i.intValue());
}
static void operate(Integer i) {
int val = i.intValue();
val += 3;
i = new Integer(val);
System.out.println("i=" + i);
}
}
main方法输出结果:
0
operate方法输出结果:
i=3
测试案例五
/**
* @author 欢迎加入Java技术交流群:646766275
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 8;
operate(i);
System.out.println(i);
}
public static void operate(int i) {
i = 9;
}
}
main方法输出结果:
8
测试案例六
/**
* @author 欢迎加入Java技术交流群:646766275
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int s = 0;
Integer c = 0;
operate(s, c);
System.out.println(s + " " + c);
}
public static void operate(int s, Integer c) {
s = s + 1;
c = s;
System.out.println(s + " " + c);
}
}
main方法输出结果:
0 0
operate方法输出结果:
1 1
测试案例七
/**
* @author 欢迎加入Java技术交流群:646766275
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
User user = new User();
user.setName("A");
setUser(user);
System.out.println(user.getName());
}
public static void setUser(User user) {
user.setName("B");
}
}
class User {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
main方法输出结果:
B
测试案例八
/**
* @author 欢迎加入Java技术交流群:646766275
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str= "123";
List<String> list= new ArrayList<>();
list.add(str);
operate(str, list);
System.out.println(str);
System.out.println(list.get(0));
}
public static void operate(String str, List<String> list) {
str= "456";
list.clear();
list.add(str);
}
}
main方法输出结果:
123
456