栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Java中是按值传递数组还是按引用传递数组?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Java中是按值传递数组还是按引用传递数组?

Everything in Java are passed-by value.
。如果是Array(只不过是Object),则数组引用按值传递。(就像对象引用按值传递)。

当你将数组传递给其他方法时,实际上是复制对该数组的引用。

  • 通过该引用对数组内容进行的任何更改都会影响原始数组。
  • 但是,将引用更改为指向新数组不会更改原始方法中的现有引用。
    看到这篇文章。

Java是“按引用传递”还是“按值传递”?

请参见以下工作示例:-

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..}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/393823.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号