首页 > 其他分享 >值传递和引用传递

值传递和引用传递

时间:2022-08-19 09:45:11浏览次数:53  
标签:num1 int System 传递 引用 println public out

public class 值传递引用传递 {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 20;

        swap(num1,num2);

        System.out.println(num1);
        System.out.println(num2);
    }
    public static void swap(int a,int b)
    {
        int temp = a;
        a = b;
        b = temp;

        System.out.println(a);
        System.out.println(b);
    }
    //可以看出,虽然在swap()方法中a,b的值做了交换,但是主方法中num1,num2的值并未改变。
}
public class ArrayListDemo {

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};

        change(arr);

        System.out.println(arr[0]);
    }

    public static void change(int[] array) {
        System.out.println(array[0]);
        array[0] = 0;
    }
}


结果:
1
0

 

标签:num1,int,System,传递,引用,println,public,out
From: https://www.cnblogs.com/shuangshuangzi/p/16600950.html

相关文章