gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:36:23 25 4
gpt4 key购买 nike

为什么下面的代码输出等于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;
}


}

最佳答案

它输出0因为这就是 a 的值在 main功能。 Java完全是按值传递的,它根本没有按引用传递。所以当你调用foo时, a变量被传递到 foo ,不是对 a 的引用多变的。 foo然后修改 its a 的值论点,但这对 a 没有任何影响变量 main .

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

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

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

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

那么在哪里:

int a = 42;

在内存中给我们这个:

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

this

Object a = new Object();

...给我们这个:

+---------+|    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;

给我们

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

and so similarly

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

给我们

+----------+     +----------+|     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;

给我们:

+----------+     +----------+|     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;
}

打印 42 。为什么?因为 a被传递到 foo ,等等b foo内指向 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 分配了一个值(新的对象引用) ,这对 a 所持有的值没有影响或者a的东西的值指的是。

关于java - java中的引用传递 - 编辑另一个函数中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9309222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com