publicclassMain{publicstaticvoidmain(String[] args){int x =3;
Main.swap(x);
System.out.println(x);}staticvoidswap(int x){
x =10;}}
函数中的值不影响函数外的值
3. 引用传参
publicclassMain{publicstaticvoidmain(String[] args){int[] x =newint[10];
x[0]=10;
Main.swap(x);
System.out.println(x[0]);}staticvoidswap(int[] x){
x[0]=12;}}
结果为12
3.1. 修改引用的部分
package com.wuqi.p1;import com.wuqi.p2.User;publicclassPassTest2{publicstaticvoidmain(String[] args){
User user =newUser();
user.setName("wutianqi");//传递对象,因为是将指向User的引用user传递给了param,//在函数中param.setName会反应到真实的对象中去。pass(user);
System.out.println("my name is "+ user.getName());}privatestaticvoidpass(User param){
param.setName("wuqi");
System.out.println("my name is "+ param.getName());}}
3.2. 修改引用
package com.wuqi.p1;publicclassPassTest3{publicstaticvoidmain(String[] args){
String name ="wutianqi";//这里传递字符串参数,按照我们以前的观点这里应该传递的是将指向字符串的name引用//传递给param,那么在pass函数中修改参数的值会直接影响到name引用指向的字符串//的值,那么输出的结果依此应该是 my name is wuqi my name is wuqipass(name);
System.out.println("my name is "+ name);}privatestaticvoidpass(String param){
param ="wuqi";
System.out.println("my name is "+ param);}}