首页 > 代码库 > Java中的参数传递
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;
}
Java中的参数传递
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。