简体   繁体   English

如何在Java中按值传递变量?

[英]How can I pass a variable by value in Java?

I'm new too Java, but I'm very experienced with PHP. 我也是Java的新手,但是我对PHP很有经验。 When I was writing a Java program, I noticed that the variables aren't what I'm expecting. 在编写Java程序时,我注意到变量不是我所期望的。

All method parameters are passed by value in Java. 所有方法参数均通过Java中的值传递。 However, since all Objects are actually references, you're passing the value of the reference when you pass an object. 但是,由于所有对象实际上都是引用,因此在传递对象时要传递引用的值。 This means if you manipulate an object passed into a method, the manipulations will stick. 这意味着,如果您对传递给方法的对象进行操作,则这些操作将卡住。

From here: What are the differences between PHP and Java? 从这里开始: PHP和Java有什么区别?

What does this mean? 这是什么意思? Does it mean that foo=blah in Java is like $foo=&$blah in PHP? 这是否意味着Java中的foo=blah就像PHP中的$foo=&$blah How can I pass just the value in Java? 如何只传递Java中的值?

What it means is, given 给出的意思是

 class Foo { 
     private int bar; 

     public void setBar(int value) {
         bar = value;
     }

     public int getBar() {
         return bar;
     }
 }

And

public void doSomethingToFoo(Foo foo) {
     foo.setBar(42);
}

The change to bar would be visible at the callsite. bar的更改将在呼叫站点上可见。 This is the manipulation that sticks. 是坚持的操作。 The reference to the original object was passed in. The setter was invoked for that object, so the getter at the callsite would return the new value of bar. 传递了对原始对象的引用。为该对象调用了setter,因此调用位置的getter将返回bar的新值。

Foo foo = new Foo();
doSomethingToFoo(foo);
int bar = foo.getBar(); // gets 42

However, given 但是,鉴于

public void doSomethingElseWithFoo(Foo foo) {
     foo = new Foo(); // assigning new instance to variable
     foo.setBar(117);
}

This change would not be visible at the callsite, as the foo variable inside the method has been reassigned. 由于在该方法内部的foo变量已被重新分配,因此该更改在调用站点将可见。 The reference was merely passed by value, the variables are not actually linked or connected in any way, they simply had a value (the reference) in common. 引用仅通过值传递,变量实际上并未以任何方式链接或连接,它们只是具有一个共同的值(引用)。 We have now overwritten the reference that one variable had inside the method, the variable at the callsite has the same reference it had before. 现在,我们已经覆盖了方法内部具有一个变量的引用,调用站点上的变量具有与以前相同的引用。

Foo foo = new Foo();
foo.setBar(7);
doSomethingElseWithFoo(foo);
int bar = foo.getBar(); // still is 7!

Does that make sense? 那有意义吗?

Anthony Pegram has explained perfectly. 安东尼·佩格拉姆(Anthony Pegram)做出了完美的解释。 I would like to add a little. 我想补充一点。

You can never pass an Object by value. 您永远不能按值传递对象。

In java every Object is created in heap. 在Java中,每个对象都是在堆中创建的。 You have reference or a what we can say a ladder to go to the Object. 您有参考或可以说是通往对象的阶梯。 In case of a function call, you pass the reference (by value) as parameter. 如果是函数调用,则将引用(按值)作为参数传递。 So you have another reference or ladder going to the same Object in your function. 因此,您在函数中有另一个引用或梯形图转到相同的对象。 You can go to the Object by this reference and change it. 您可以通过此引用转到“对象”并进行更改。 Now if you change the reference in your function like Anthony did 现在,如果您像Anthony一样更改函数中的引用

 foo = new Foo();

what happens, you just created a new Object in heap and you have the reference of that object.In this step, you replace your existing reference(Reference to old Object) by the reference of newly created object. 发生了什么,您刚刚在堆中创建了一个新对象,并且有了该对象的引用。在此步骤中,您将现有引用(对旧对象的引用)替换为新创建的对象的引用。 So in your function, you lost the way to go to the old Object.Now if you do any change, you are changing the newly created object, not the Object of caller function. 因此,在您的函数中,您迷路了前往旧的Object的位置。现在,如果您进行任何更改,那么您将更改的是新创建的对象,而不是调用方函数的Object。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM