是数组按值传递或引用在Java中传递? [英] Are arrays passed by value or passed by reference in Java?

查看:350
本文介绍了是数组按值传递或引用在Java中传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  是Java的传递通过引用?

数组是不是一个原始类型,但他们are不是对象要么,所以他们是通过了由值或引用?它依靠什么数组包含,例如引用或基本类型?

Arrays are not a primitive type in Java, but they are not objects either, so are they passed by value or by reference? Does it depend on what the array contains, for example references or a primitive type?

推荐答案

从技术上讲,它是按值传递......但价值的参考阵列。

Technically, it is passed by value ... but the value is the reference to the array.

实传递通过引用包括使变量的地址,以便将变量可被更新。这是不是当你通过Java中的数组会发生什么。

Real passing by reference involves passing the address of a variable so that the variable can be updated. This is NOT what happens when you pass an array in Java.

下面是一些链接,解释之间的区别传递通过引用和传递按值:

Here are some links that explain the difference between "pass-by-reference" and "pass-by-value":

相关SO问题:

  • Is Java "pass-by-reference"?

历史背景:

短语传递通过引用原是呼叫通过引用,它是用来区分那些通过FORTRAN(呼叫按引用)的语义参数ALGOL-60(call-按值和呼叫按姓名)。

The phrase "pass-by-reference" was originally "call-by-reference", and it was used to distinguish the argument passing semantics of FORTRAN (call-by-reference) from those of ALGOL-60 (call-by-value and call-by-name).


  • 在呼叫按值,参数前pression进行评估,以一个值,并且该值被复制到被调用的方法。

  • In call-by-value, the argument expression is evaluated to a value, and that value is copied to the called method.

在的call-by-参考,参数前pression部分评估,以传递给调用方法的左值(即一个变量或数组元素的地址)。然后调用方法可以直接读取和更新变量/元素。

In call-by-reference, the argument expression is partially evaluated to an "lvalue" (i.e. the address of a variable or array element) that is passed to the calling method. The calling method can then directly read and update the variable / element.

在的call-by-名,实际参数前pression传递给调用方法(!!),它可以在多次评估(!)。这是复杂的实现,并且可以使用(滥用)来写code,这是很难理解的。通话按名字永远只能在使用的Algol-60(谢天谢地!)。

In call-by-name, the actual argument expression is passed to the calling method (!!) which can evaluate it multiple times (!!!). This was complicated to implement, and could be used (abused) to write code that was very difficult to understand. Call-by-name was only ever used in Algol-60 (thankfully!).

更新

其实,大陵60年代的call-by-name是类似于将lambda前pressions作为参数。皱纹是,这些未准确-λ-EX pressions(他们被称为thunk出现在执行层面)可以的间接的修改都在范围内的变量的状态调用过程/函数。那是什么让他们如此难以理解。 (见 Jensen的例如设备维基百科页面。)

Actually, Algol-60's call-by-name is similar to passing lambda expressions as parameters. The wrinkle is that these not-exactly-lambda-expressions (they were referred to as "thunks" at the implementation level) can indirectly modify the state of variables that are in scope in the calling procedure / function. That is what made them so hard to understand. (See the Wikipedia page on Jensen's Device for example.)

这篇关于是数组按值传递或引用在Java中传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆