Java中的实例变量是通过引用传递的吗?
现在我知道Java是纯粹按值传递的,但是实例变量是按引用传递的吗?
这就是我的意思(我知道这段代码很糟糕,但它是伪代码:
//Instance variables
private Object[] array = new Object[10];
array[4] = new Object[5];
//Private method
private Object ar(int x)
{
return array[x];
}
//Inside Main or some other method
ar(4)[0] = "Foo";
现在,将 array[4]
中数组的第一个槽更改为“Foo”,因为 array
是一个实例变量吗?
澄清一下:
我知道一切都是按值传递的,但我们正在讨论调用实例变量中包含的内容,请重点关注。谢谢。
Now I know that Java is purely passed-by-value, but are instance variables passed by reference?
Here's what I mean (and I know this code is terrible, but it's pseudo-code:
//Instance variables
private Object[] array = new Object[10];
array[4] = new Object[5];
//Private method
private Object ar(int x)
{
return array[x];
}
//Inside Main or some other method
ar(4)[0] = "Foo";
Now, would the first slot on the array in array[4]
be changed to "Foo" because array
is an instance variable?
To Clarify:
I know that EVERYTHING is passed by value. But we are talking about calling things contained in instance variables, please focus on that. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
是的,
array[4]
中的第一个槽将更改为"Foo"
在 Java 中,一切都是按值传递的。传递对象时,对象的引用是按值传递的。对于您的示例,
array[4]
中包含的对象是从ar
方法返回的。Yes, the first slot in
array[4]
will be changed to"Foo"
In Java, everything is passed by value. When passing objects, the reference to the object is passed by value. For your example, the object contained in
array[4]
is returned from thear
method.变量永远不会被传递。仅它们包含的值。也就是说:如果它是实例变量或其他变量,则没有区别(至少为了争论“传递了什么”)。
请记住,对象就是它是什么,并且不存在对象的隐式复制/克隆/重复——这包括数组对象——它们本身(将基元视为自我表示的不可变实体) 。
快乐编码。
更新:我找到了这个链接并且喜欢它。 按对象调用。这个术语是由利斯科夫提出的。
这就是差异的总和:
“共享调用”是通过“传递引用的值”来实现的——不需要考虑描述调用语义、对象或对象的实现细节-身份。。
Variables are never passed. Only the values that they contain. That is: it makes no difference if it is an instance variable or otherwise (at least for sake of arguing "what is passed").
Remember that an object is-what-it-is and there is no implicit copy/clone/duplicate of objects -- this includes array objects -- themselves (consider primitives to be self-representing immutable entities).
Happy coding.
Update: I found this link and I like it. Call by object. This term is attributed to Liskov.
This sums the differences:
Only in Java the "call-by-sharing" is implemented with "passing the value of the reference" -- an implementation detail that doesn't need to be considered to describe calling semantics, objects, or object-identity.
来自 Java 的作者:“Java 中只有一种参数传递模式 - 按值传递 - 这有助于使事情变得简单。” Java 编程语言,第二版。作者:Ken Arnold 和 James Gosling,第 2.6.1 节,第 40 页,第 3 段。
From the authors of Java: "There is exactly one parameter passing mode in Java - pass by value - and that helps keep things simple." The Java Programming Language, 2nd ed. by Ken Arnold and James Gosling, section 2.6.1, page 40, 3rd paragraph.
Java 语言规范: http://java.sun.com /docs/books/jls/third_edition/html/typesValues.html#4.1
§4.1 有...两种数据值可以...作为参数传递.. .: 原始值 (§4.2) 和引用值
§4.3 引用值(通常只是引用)是指向这些对象的指针
所以 Java 是通过“值”,或“参考值”,或“引用”,或“指针”。 (忽略原语)
所有 4 个版本均受到语言规范的认可。
现在我们可以停止这个文字游戏了吗?我们都知道过去的事情,这还不够吗?我们称之为路过玫瑰怎么样?
Java Lang Spec: http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.1
§4.1 There are ... two kinds of data values that can be ... passed as arguments...: primitive values (§4.2) and reference values
§4.3 The reference values (often just references) are pointers to these objects
So Java is pass by "value", or "reference value", or "reference", or "pointer". (ignoring primitives)
All 4 versions are sanctioned by the language spec.
Now can we please stop this word game. We all know what is passed, isn't that enough? What about we call it pass by rose?
是的,该代码会将
array[4][0]
设置为“Foo”。array
是一个实例变量这一事实是无关紧要的。Yes, that code will set
array[4][0]
to "Foo". The fact thatarray
is an instance variable is irrelevant.好吧,我通过运行一个非常简单的测试来回答我自己的问题,但是已经留下的一些答案有助于澄清很多事情。我知道大约 14 种编程语言,所以什么语言做什么有时会变得有点模糊。
输出:
感谢您的帮助!
Well, I sort of answered my own question by running a pretty simple test, but some of the answers that have already been left helped clarify things a lot. I know something like 14 programming languages, so what language does what sometimes gets a little fuzzy.
outputs:
Thanks for all the help!
一切都包括“实例变量中包含的内容”。
这当然是显而易见的吗?
EVERYTHING includes 'things contained in instance variables'.
Surely that is obvious?
考虑它的最好方法是变量的地址正在被传递,除非它是一个原语。 (数组被视为非原始数组)。这将引导您从运行测试程序中得到答案,并解释原因。
(术语“按引用”、“按值”、“实例”在这种情况下是不明确且令人困惑的。)
the best way to think about it is that addresses of variables are being passed around unless it is a primitive. (with arrays being treated as non-primitive). this would lead you to the answer you got from running your test program and also explain why.
(the terms "by-reference", "by-value", "instance" are ambiguous and confusing in this context.)