[英]Java: pass-by-value references in methods
I read a few articles and similar question in Stackoverflow; 我在Stackoverflow中阅读了几篇文章和类似的问题; however, I did not quite get an answer to my question.
但是,我并没有完全回答我的问题。 Here is the code:
这是代码:
public class CoinFlipping {
Random random = new Random();
Boolean head = null;
public void flip(Boolean b){
b = random.nextBoolean();
// head = b;
}
public static void main(String [] args){
CoinFlipping cf = new CoinFlipping();
cf.flip(cf.head);
System.out.println("Head: "+cf.head);
}
}
I refer to this arcticle: Is Java "pass-by-reference" or "pass-by-value"? 我指的是这个细节: Java是“按引用传递”还是“按值传递”? I can understand why that piece of code behaves as it does.
我能理解为什么这段代码会如此表现。 When we call
new dog()
we basically create a new Dog object. 当我们调用
new dog()
我们基本上创建了一个新的Dog对象。 I get it. 我知道了。 However, the example that I provided seems to be confusing a bit.
但是,我提供的示例似乎有些混乱。 I am not creating an object in this case (aren't I?).
在这种情况下,我没有创建对象(不是吗?)。 I am changing a value.
我正在更改一个值。 Then why in this case when I print results, I get
null
and not true
or false
? 那么,为什么在这种情况下,当我打印结果时却得到
null
而不是true
或false
?
The parameter b
is passed by value, even though b
is itself a reference. 即使
b
本身是引用,参数b
也会按值传递。 Thus, when you call flip
, the flip
method treats b
as a local variable that is a copy of what you pass into it. 因此,当您调用
flip
, flip
方法将b
视为局部变量,该局部变量是传递给它的内容的副本 。 Changing this copy doesn't change cf.head
. 更改此副本不会更改
cf.head
。
If b
were a reference to a mutable object, you could change the object b
refers to, and that change would be seen by the rest of the program after flip
returns. 如果
b
是对可变对象的引用,则可以更改b
引用的对象,并且该变化将在flip
返回之后由程序的其余部分看到。 But a change to b
itself is a change to a copy, and any such change will not be seen by the rest of the program. 但是对
b
本身的更改就是对副本的更改,并且程序的其余部分将看不到任何此类更改。 ( Boolean
is not mutable.) (
Boolean
是不可变的。)
To answer your question: 要回答您的问题:
Then why in this case when I print results, I get
null and not
true or
false ?
那么,为什么在这种情况下,当我打印结果时却得到null而不是
true或
false ?
I have created a step-by-step walktrough of your code to show you the pointers (references) of the specific variables and answer why it prints out
null
in the end: 我创建了代码的逐步介绍 ,以向您显示特定变量的指针(引用),并回答为什么最终将其打印为
null
:
1. Step: New CoinfFlipping object is created with default field values
1.步骤:使用默认字段值创建新的CoinfFlipping对象
CoinFlipping cf = new CoinFlipping();
Inside the CoinFlipping
class: 在
CoinFlipping
类中:
Random random = new Random(); Boolean head = null;
cf
---- points to ----> CoinFlipping
object cf
----指向----> CoinFlipping
对象
cf.random
---- points to ----> Random
object cf.random
----指向----> Random
对象
cf.head
---- points to ----> null
cf.head
----指向----> null
2. Step: Calling the
flip
method with cf.head
as parameter on the cf
object 2.步骤:在
cf
对象上使用cf.head
作为参数调用flip
方法
cf.flip(cf.head);
When evoking this method, a variable b
is created inside the scope of the flip
method. 调用此方法时,将在
flip
方法的范围内创建一个变量b
。 The pointer (reference) from cf.head
is copied into the variable b
. 来自
cf.head
的指针(引用)被复制到变量b
。 So cf.head
and b
point then at the same thing. 因此,
cf.head
和b
指向同一件事。
public void flip(Boolean b){
cf
---- points to ----> CoinFlipping
object cf
----指向----> CoinFlipping
对象
cf.random
---- points to ----> Random
object cf.random
----指向----> Random
对象
cf.head
---- points to ----> null
cf.head
----指向----> null
b
---- points to ----> null
b
----指向----> null
3. Step: Inside the
flip
method b
is being reassigned 3.步骤:在
flip
方法内部b
被重新分配
b = random.nextBoolean();
cf
---- points to ----> CoinFlipping
object cf
----指向----> CoinFlipping
对象
cf.random
---- points to ----> Random
object cf.random
----指向----> Random
对象
cf.head
---- points to ----> null
cf.head
----指向----> null
b
---- points to ----> a boolean
value - either true
or false
b
----指向----> boolean
值true
或false
4. Step: Back in the main method to print out
cf.head
4.步骤:返回主要方法以打印
cf.head
System.out.println("Head: "+cf.head);
cf
---- points to ----> CoinFlipping
object cf
----指向----> CoinFlipping
对象
cf.random
---- points to ----> Random
object cf.random
----指向----> Random
对象
cf.head
---- points to ----> null
cf.head
----指向----> null
b
---- points to ----> a boolean
value - either true
or false
b
----指向----> boolean
值true
或false
As you can see,
cf.head
is still pointing to null
, therefore null
is being printed. 如您所见,
cf.head
仍指向null
,因此正在打印null
。
Solution: 解:
To change this, you could let the method return a boolean value instead and assign that to cf.head
in order to make cf.head
point to a new boolean value. 要更改此设置,可以让该方法返回一个布尔值,然后将其分配给
cf.head
,以使cf.head
指向新的布尔值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.