Java中方法参数传递是:值传递(Pass By Value)
public
class
ParameterDemo{
public
void
changeValue1(
int
a){
a =
8
;
}
public
void
changeValue2(Employee e){
e.salary =
8000
;
}
public
void
changeValue3(Employee e){
e =
new
Employee();
e.salary =
8000
;
}
public
void
swap(Employee e, Employee e2){
Employee temp = e;
e = e2;
e2 = temp;
}
public
static
void
main(String[] args){
MethodDemo m =
new
MethodDemo();
int
a =
10
;
System.out.println(a);
//10
m.changeValue1(a);
System.out.println(a);
//10
Employee e =
new
Employee();
System.out.println(e.salary);
//0
m.changeValue2(e);
System.out.println(e.salary);
//8000
Employee e2 =
new
Employee();
System.out.println(e2.salary);
//0
m.changeValue3(e2);
System.out.println(e2.salary);
//0
Employee e3 =
new
Employee();
e3.name =
"huhu"
;
Employee e4 =
new
Employee();
e4.name =
"hengheng"
;
System.out.println(
"e3.name = "
+e3.name+
" e4.name = "
+ e4.name);
//e3.name = huhu e4.name = hengheng
m.swap(e3, e4);
System.out.println(
"e3.name = "
+e3.name+
" e4.name = "
+ e4.name);
//e3.name = huhu e4.name = hengheng
}
}
class
Employee{
String name;
int
salary;
}