Java中的参数传递
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;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。