1.int的传值(不会传值)
public class MathDemo {
public void swop(int a,int b){
int temp;
System.out.println("交换前:a的值为"+a+",b的值为"+b); //交换前:a的值为10,b的值为6
temp=a;
a=b;
b=temp;
System.out.println("交换后:a的值为"+a+",b的值为"+b); //交换后:a的值为6,b的值为10
}
public static void main(String[] ages){
MathDemo Link=new MathDemo();
int a=10,b=6;
System.out.println("前:a的值为"+a+",b的值为"+b); //前:a的值为10,b的值为6
Link.swop(a,b);
System.out.println("后:a的值为"+a+",b的值为"+b); //后:a的值为10,b的值为6
}
}
2.数组的传值(会传值)
public class MathDemo {
public void ArrayDemo(int[] arr){
arr[2]=20;
System.out.println("在方法里arr数组元素:");
//循环打印出数组
for(int n:arr){
System.out.println(n); //2 3 20 8
}
}
public static void main(String[] ages){
MathDemo myDemo=new MathDemo();
int[] arr={2,3,4,8};
System.out.println("数组arr之前的元素为:");
//循环打印出数组
for(int i:arr){
System.out.println(i); //2 3 4 8
}
myDemo.ArrayDemo(arr);
System.out.println("数组arr之后的元素为:");
//循环打印出数组
for(int j:arr){
System.out.println(j); //2 3 20 8
}
}
}