不同类型参数为传值,被调用前后的改变
public class ParameterChangeTest {
//类方法1:integer和boolean类型参数为传值,调用前后没有发生改变。
static void funcA(int c,boolean b){
c++;
b=false;
System.out.println("在funcA为:"+c+"-"+b);
}
//类方法2:数组与字符串对象参数为传址,调用前后数组发生了改变,String对象没有发生改变。
static void funcB(int temp[],String a){
temp[1]=150;
a="江小余";
System.out.println("在funcB为:"+temp[1]+"-"+a);
}
//主程序
public static void main(String[] args) {
int c=1;
boolean b=true;
String str="南小风";
int arr[]={1,2,3};
System.out.println("调用funcA前:"+c+"-"+b);
//调用类方法
funcA(c,b);
System.out.println("调用funcA后:"+c+"-"+b);
System.out.println("调用funcB前:"+arr[1]+"-"+str);
//调用类方法
funcB(arr,str);
System.out.println("调用funcB后:"+arr[1]+"-"+str);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/a4a9dc0ea882559c06633fefb0e6484d.jpeg)
- integer和boolean类型参数为传值,调用前后没有发生改变。
- 数组与字符串对象参数为传址,调用前后数组发生了改变,String对象没有发生改变。