java方法的传值问题

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
        }

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值