java 数组引用传递_在Java中数组是通过值传递还是通过引用传递?

Everything in Java are passed-by value...对于Array(它只不过是一个对象),数组引用是通过值传递的。(就像通过值传递对象引用一样)。

当您将数组传递给其他方法时,实际上复制了对该数组的引用。通过该引用对数组内容的任何更改都将影响原始数组。

但是,将引用更改为指向新数组并不会更改原始方法中现有的引用。

看这个帖子.。

参见下面的工作示例:-public static void changeContent(int[] arr) {

// If we change the content of arr.

arr[0] = 10;  // Will change the content of array in main()}public static void changeRef(int[] arr) {

// If we change the reference

arr = new int[2];  // Will not change the array in main()

arr[0] = 15;}public static void main(String[] args) {

int [] arr = new int[2];

arr[0] = 4;

arr[1] = 5;

changeContent(arr);

System.out.println(arr[0]);  // Will print 10..

changeRef(arr);

System.out.println(arr[0]);  // Will still print 10..

// Change the reference doesn't reflect change here..}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值