Java中的实例变量是通过引用传递的吗?

发布于 2024-10-21 02:09:56 字数 490 浏览 3 评论 0原文

现在我知道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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

失眠症患者 2024-10-28 02:09:56

是的,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 the ar method.

寄风 2024-10-28 02:09:56

变量永远不会被传递。仅它们包含的值。也就是说:如果它是实例变量或其他变量,则没有区别(至少为了争论“传递了什么”)。

请记住,对象就是它是什么,并且不存在对象的隐式复制/克隆/重复——这包括数组对象——它们本身(将基元视为自我表示的不可变实体) 。

快乐编码。


更新:我找到了这个链接并且喜欢它。 按对象调用。这个术语是由利斯科夫提出的。

<块引用>

但是没关系,你唯一需要知道的是Python的 Java的模型既不是“按值调用”也不是“按引用调用”(因为任何使用的尝试> [用这些术语思考][用这些术语思考]...)最准确的描述是 CLU 的“通过对象调用”“通过共享调用”。或者,如果您愿意,“通过对象引用调用”。

这就是差异的总和:

<块引用>

“特别是它不是按值调用,因为突变
被调用例程执行的参数将对
来电者。它不是通过引用调用,因为访问
不提供给调用者的变量,而仅提供给某些
对象。”

“共享调用”是通过“传递引用的值”来实现的——不需要考虑描述调用语义、对象或对象的实现细节-身份。

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.

But nevermind, the only thing you need to know is that Python’s Java's model is neither “call by value” nor “call by reference” (because any attempt to use [think in] those terms [is thinking in those terms]...) The most accurate description is CLU’s “call by object” or “call by sharing“. Or, if you prefer, “call by object reference“.

This sums the differences:

"IN PARTICULAR IT IS NOT CALL BY VALUE because mutations
of arguments performed by the called routine will be visible to
the caller. And IT IS NOT CALL BY REFERENCE because access
is not given to the variables of the caller, but merely to certain
objects."

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.

纵性 2024-10-28 02:09:56

来自 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.

巨坚强 2024-10-28 02:09:56

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?

堇色安年 2024-10-28 02:09:56

是的,该代码会将 array[4][0] 设置为“Foo”。 array 是一个实例变量这一事实是无关紧要的。

Yes, that code will set array[4][0] to "Foo". The fact that array is an instance variable is irrelevant.

蝶舞 2024-10-28 02:09:56

好吧,我通过运行一个非常简单的测试来回答我自己的问题,但是已经留下的一些答案有助于澄清很多事情。我知道大约 14 种编程语言,所以什么语言做什么有时会变得有点模糊。

public class ArrayPassTest {

    private Object[] array;

    public ArrayPassTest()
    {
        array = new Object[10];
        array[4] = new Object[10];
    }

    public Object[] ar(int x)
    {
        return (Object[])array[x];
    }

    public static void main(String[] args) {
        ArrayPassTest apt = new ArrayPassTest();

        apt.ar(4)[0] = "Foo";

        System.out.println(apt.ar(4)[0]);
        System.out.println(apt.ar(4)[5]);

    }

}

输出:

Foo
null

感谢您的帮助!

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.

public class ArrayPassTest {

    private Object[] array;

    public ArrayPassTest()
    {
        array = new Object[10];
        array[4] = new Object[10];
    }

    public Object[] ar(int x)
    {
        return (Object[])array[x];
    }

    public static void main(String[] args) {
        ArrayPassTest apt = new ArrayPassTest();

        apt.ar(4)[0] = "Foo";

        System.out.println(apt.ar(4)[0]);
        System.out.println(apt.ar(4)[5]);

    }

}

outputs:

Foo
null

Thanks for all the help!

梦在深巷 2024-10-28 02:09:56

我知道一切都会过去
价值。但我们正在谈论
调用实例中包含的东西
变量,请重点关注。
谢谢。

一切都包括“实例变量中包含的内容”。

这当然是显而易见的吗?

I know that EVERYTHING is passed by
value. But we are talking about
calling things contained in instance
variables, please focus on that.
Thanks.

EVERYTHING includes 'things contained in instance variables'.

Surely that is obvious?

陌上芳菲 2024-10-28 02:09:56

考虑它的最好方法是变量的地址正在被传递,除非它是一个原语。 (数组被视为非原始数组)。这将引导您从运行测试程序中得到答案,并解释原因。

(术语“按引用”、“按值”、“实例”在这种情况下是不明确且令人困惑的。)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文