java中的引用传递 - 编辑另一个函数中的值

发布于 2025-01-05 18:39:09 字数 241 浏览 2 评论 0原文

为什么下面的代码输出等于0?

public class A{


    public static void main(String [] args){

        int a=0;
        foo(a);
        System.out.println(a);
    }

    public static void foo(int a){
        a=78;
    }


}

why is the following code output equals 0?

public class A{


    public static void main(String [] args){

        int a=0;
        foo(a);
        System.out.println(a);
    }

    public static void foo(int a){
        a=78;
    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

巾帼英雄 2025-01-12 18:39:09

它输出 0,因为这是 main 函数中 a 的值。 Java完全是按值传递的,它根本没有按引用传递。因此,当您调用 foo 时,a 变量的会传递到 foo 中,而不是对一个变量。 foo 然后修改 a 参数的值,但这对 a 变量没有任何影响 <代码>主。

您可能会听到人们谈论“按值传递引用”或“按引用值传递”,这可能会令人困惑,我强烈建议像瘟疫一样避免使用这些术语。 :-) 但这就是他们谈论的内容:

在您的示例中,a 是一个基元a 变量包含0。但同样,您可以这样做:

 Object a = new Object();
 foo(a);

现在将什么传递给 foo完全int 时传递的内容相同:a值代码>.唯一的区别是,现在 a是一个对象引用。对象引用只是像其他任何东西一样的值;它们告诉 JVM 对象的数据在内存中的位置。将它们想象成一个大数组或其他东西的索引。

所以 where:

int a = 42;

在内存中给了我们这个:

+---------+
|    a    |
+---------+
|   42    |
+---------+

this

Object a = new Object();

...给了我们这个:

+---------+
|    a    |             
+---------+             +-----------------+
| mumble  |------------>| The object data |
+---------+             +-----------------+

重要的是要理解 a 仍然只包含一个值,就像它包含一个 int 一样或浮点。只是该值的含义不同,就像int所持有的值的含义与float所持有的值的含义不同一样。在对象引用的情况下,该值的含义是“对象在那边”。

就像您可以有多个变量保存值42一样,您也可以有多个变量保存对同一对象的引用:

int a, b;
a = 42;
b = a;

给我们

+---------+     +---------+
|    a    |     |    b    |
+---------+     +---------+
|   42    |     |   42    |
+---------+     +---------+

,因此类似地

Object a, b;
a = new Object();
b = a;

给我们

+----------+     +----------+
|     a    |     |     b    |
+----------+     +----------+
|  mumble  |--+--|  mumble  |
+----------+  |  +----------+
              |
              |
              |    +-----------------+
              +--->| The object data |
                   +-----------------+

ab 仍然只包含值,例如 42;但该值告诉 JVM 该对象在哪里,因此它们引用(指向)同一个对象。

回到foo,当你调用一个函数时,传入一个参数完全就像将一个变量分配给另一个变量,所以想想b上面作为 foo 函数参数。

在下面的评论中,您询问了有关数组的问题。数组就像对象一样——或者它们对象;选择您喜欢的语义。 :-) 所以:

int[] a;
int[] b;

a = new int[5];
b = a;

给我们:

+----------+     +----------+
|     a    |     |     b    |
+----------+     +----------+
|  mumble  |--+--|  mumble  |
+----------+  |  +----------+
              |
              |
              |    +----------------+
              +--->| The array data |
                   +----------------|
                   | [0]: 0         |
                   | [1]: 0         |
                   | [2]: 0         |
                   | [3]: 0         |
                   | [4]: 0         |
                   +----------------+

所以考虑一下:

public static final void main(String[] args) {
    int[] a = new int[5];
    foo(a);
    System.out.println(a[0]);
}

void foo(int[] b) {
    b[0] = 42;
}

打印 42。为什么?因为a被传递到foo中,所以bfoo中指向 a 指向的同一个数组。您可以使用引用更改对象的状态。请注意,您并不是在更改 b 的值,而是在更改 b 所引用的事物的状态。

对比:

public static final void main(String[] args) {
    int[] a = new int[5];
    foo(a);
    System.out.println(a[0]);
}

void foo(int[] b) {
    b = new int[5]; // <==== change here
    b[0] = 42;
}

现在它打印 0,因为你已经为 b 分配了一个 new 值(一个新的对象引用),这没有任何效果a 所持有的值或 a 的值所引用的事物。

It outputs 0 because that's the value of a in the main function. Java is entirely pass-by-value, it doesn't have pass-by-reference at all. So when you call foo, the value of the a variable is passed into foo, not a reference to the a variable. foo then modifies the value of its a argument, but that has no effect whatsoever on the a variable in main.

You may hear people talk about "pass-references-by-value" or "pass-by-value-of-reference," which can be confusing and I strongly recommend avoiding that terminology like the plague. :-) But here's what they're talking about:

In your example, a is a primitive. The a variable contains the value 0. But equally, you could do this:

 Object a = new Object();
 foo(a);

What gets passed to foo now? Exactly the same thing that was passed when it was an int: The value of a. The only difference is that now, the value of a is an object reference. Object references are just values like anything else; they tell the JVM where the data for the object is in memory. Think of them like indexes into a big array or something.

So where:

int a = 42;

gives us this in memory:

+---------+
|    a    |
+---------+
|   42    |
+---------+

this

Object a = new Object();

...gives us this:

+---------+
|    a    |             
+---------+             +-----------------+
| mumble  |------------>| The object data |
+---------+             +-----------------+

It's important to understand that a still just contains a value, just like when it contains an int or a float. It's just the meaning of that value which differs, just as the meaning of the value held by an int is different from the meaning of a value held by a float. In the case of an object reference, the meaning of the value is "the object is over there".

Just like you can have more than one variable holding the value 42, you can have more than one variable holding a reference to the same object:

int a, b;
a = 42;
b = a;

gives us

+---------+     +---------+
|    a    |     |    b    |
+---------+     +---------+
|   42    |     |   42    |
+---------+     +---------+

and so similarly

Object a, b;
a = new Object();
b = a;

gives us

+----------+     +----------+
|     a    |     |     b    |
+----------+     +----------+
|  mumble  |--+--|  mumble  |
+----------+  |  +----------+
              |
              |
              |    +-----------------+
              +--->| The object data |
                   +-----------------+

a and b still just contain values, like 42; but that value tells the JVM where the object is, and so they refer to (point to) the same object.

Getting back to foo, when you call a function, passing in an argument is exactly like assigning a variable to another variable, so think of b above as the foo function argument.

In the comments below, you've asked about arrays. Arrays are just like objects — or they are objects; choose your preferred semantics. :-) So:

int[] a;
int[] b;

a = new int[5];
b = a;

gives us:

+----------+     +----------+
|     a    |     |     b    |
+----------+     +----------+
|  mumble  |--+--|  mumble  |
+----------+  |  +----------+
              |
              |
              |    +----------------+
              +--->| The array data |
                   +----------------|
                   | [0]: 0         |
                   | [1]: 0         |
                   | [2]: 0         |
                   | [3]: 0         |
                   | [4]: 0         |
                   +----------------+

So consider:

public static final void main(String[] args) {
    int[] a = new int[5];
    foo(a);
    System.out.println(a[0]);
}

void foo(int[] b) {
    b[0] = 42;
}

That prints 42. Why? Because the value of a was passed into foo, and so b within foo points to the same array that a points to. You can change the state of the object using the reference. Note that you're not changing the value of b, you're changing the state of the thing that b refers to.

Contrast with:

public static final void main(String[] args) {
    int[] a = new int[5];
    foo(a);
    System.out.println(a[0]);
}

void foo(int[] b) {
    b = new int[5]; // <==== change here
    b[0] = 42;
}

Now it prints 0, because you've assigned a new value (a new object reference) to b, which has no effect on the value held by a or the thing that a's value refers to.

撞了怀 2025-01-12 18:39:09

因为Java严格来说是“按值传递”。请注意仔细:按值传递引用与“按引用传递”不同。

Because Java is strictly "Pass by value". And note carefully: Passing a reference by value is not the same as "Pass by Reference".

高冷爸爸 2025-01-12 18:39:09

Java 中的参数始终按值传递。
foo 中的参数 amain 中的可用 a 没有连接。

Arguments in Java are always passed by value.
Argument a in foo is not connected with vaiable a in main.

Bonjour°[大白 2025-01-12 18:39:09

是啊。
因为java都是按值传递
i 是一种原始类型,因此传递的是他的值,即 0 而不是他的引用。

您可以阅读下面的链接轻松了解java中参数的传递方式

http://www.javaranch。 com/campfire/StoryPassBy.jsp

Ya.
Because java is all pass by value
and i is a primitive type so the one that passed is his value which is 0 not his reference.

You can read the link below to easily understand how parameter are passed in java

http://www.javaranch.com/campfire/StoryPassBy.jsp

攀登最高峰 2025-01-12 18:39:09

当您在 main() 方法中调用 foo(a) 时,它会获取 main() 方法中声明的值并将其传递给 foo() 方法的参数。

foo() 的参数将始终充当 foo() 方法的局部作用域变量。所以无论你在里面做了什么,都不会反映在 main() 方法中。因此,当您打印它时,它会显示本地 main() 方法中声明的 a 的值。

When you are calling foo(a) inside main() method, it takes the value of a declared inside main() method and passes it to the argument of foo() method.

The argument of foo() will always act as local scoped variable to foo() method. So whatever you did inside it will never reflect inside main() method. So when you print it, it shows the value of a which was declared inside main() method locally.

柠檬色的秋千 2025-01-12 18:39:09

Java 使用按值传递,而不是按引用传递。很多人都感到困惑的是,如果您正在处理一个对象(int 不是对象),那么该值实际上是对该对象的引用。

以下内容将起作用:

public class A{
    public static void main(String [] args){
        IntWrapper a = new IntWrapper(0);
        bar(a); // Pass a copy of the reference to a
        System.out.println(a.intValue);
    }

    public static void bar(IntWrapper iw) { // iw is a reference because IntWrapper is an Object
        iw.intValue = 78;
    }

    class IntWrapper { // Classes implicitly extend Object
        int intValue;

        IntWrapper(int value) {
            intValue = value;
    }
}

Java uses pass-by-value, not pass-by-reference. The confusion a lot of people suffer from is that if you're dealing with an Object (int is NOT an Object) then the value is actually a reference to the Object.

The following would work:

public class A{
    public static void main(String [] args){
        IntWrapper a = new IntWrapper(0);
        bar(a); // Pass a copy of the reference to a
        System.out.println(a.intValue);
    }

    public static void bar(IntWrapper iw) { // iw is a reference because IntWrapper is an Object
        iw.intValue = 78;
    }

    class IntWrapper { // Classes implicitly extend Object
        int intValue;

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