1值传递: int,double,Integer,Double,String
public class Test {
public static void main(String args[]){
int i1 = 1;
double d1 = 1.1;
Integer I1 = 1;
Double D1 = 1.1;
String S1 = "一";
int i2 = i1;
double d2 = d1;
Integer I2 = I1;
Double D2 = D1;
String S2 = S1;
i2 = 2;
d2 = 2.2;
I2 = 2;
D2 = 2.2;
S2 = "二";
System.out.println(i1+" "+d1+" "+I1+" "+D1+" "+S1);
}
}
输出: 1 1.1 1 1.1 一
2引用传递: 对象,List,Map
class A {
int i = 1;
double d = 1.1;
Integer I = 1;
Double D = 1.1;
String S = "一";
}
public class Test {
public static void main(String args[]){
A a = new A();
A b = a;
b.i = 2;
b.d = 2.2;
b.I = 2;
b.D = 2.2;
b.S = "二";
System.out.println(a.i+" "+a.d+" "+a.I+" "+a.D+" "+a.S);
List<Integer> list1 = new ArrayList<Integer>();
list1.add(1);
list1.add(2);
List<Integer> list2 = list1;
list2.remove(0);
System.out.println(list1.size());
HashMap<String, String> map1 = new HashMap<String, String>();
map1.put("a", "a");
map1.put("b", "b");
HashMap<String, String> map2 = map1;
map2.remove("a");
System.out.println(map1.size());
}
}
输出: 2 2.2 2 2.2 二
1
1
3引用传递转值传递
class A {
int i = 1;
double d = 1.1;
Integer I = 1;
Double D = 1.1;
String S = "一";
}
public class Test {
public static void main(String args[]){
A a = new A();
A b = new A();
try {
org.apache.commons.beanutils.BeanUtils.copyProperties(b, a);
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
b.i = 2;
b.d = 2.2;
b.I = 2;
b.D = 2.2;
b.S = "二";
System.out.println(a.i+" "+a.d+" "+a.I+" "+a.D+" "+a.S);
List<Integer> list1 = new ArrayList<Integer>();
list1.add(1);
list1.add(2);
List<Integer> list2 = new ArrayList<Integer>();
list2.addAll(list1);
list2.remove(0);
System.out.println(list1.size());
HashMap<String, String> map1 = new HashMap<String, String>();
map1.put("a", "a");
map1.put("b", "b");
HashMap<String, String> map2 = new HashMap<String, String>();
try {
org.apache.commons.beanutils.BeanUtils.copyProperties(map2, map1);
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
map2.remove("a");
System.out.println(map1.size());
}
}
输出: 1 1.1 1 1.1 一
2
2