在 Java 中赋值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3858510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assigning in Java?
提问by Brent
Say I set int A = int B. When I change A after, it will not change the value of B. But when I set a SomeClass A = SomeClass B, and I change A's contents (like a.cost), it changes B.cost as well. Can someone explain this to me?
假设我设置了 int A = int B。当我更改 A 之后,它不会更改 B 的值。但是当我设置 SomeClass A = SomeClass B,并且我更改 A 的内容(如 a.cost)时,它会更改 B .成本也是如此。谁可以给我解释一下这个?
I thought Java is assigned by value, not reference?
我以为 Java 是按值分配的,而不是按引用分配的?
回答by Jon Skeet
Yes, it does - but the value of A is a reference, not a copy of the object itself.
是的,确实如此 - 但 A 的值是一个引用,而不是对象本身的副本。
I like to give the following analogy...
我喜欢给出以下比喻...
Suppose two people both have my address: that's like two variables of type House
in Java. Now one of them comes and paints my door red. The second person will still see the red door if they visit:
假设两个人都有我的地址:这就像House
Java 中的两个类型变量。现在他们中的一个来了,把我的门漆成红色。如果第二个人访问,他们仍然会看到红色的门:
House jonsHouse = new House(); // Even the variable jonsHouse is only a reference
House firstAddressCopy = jonsHouse; // Just a copy of the reference
House secondAddressCopy = jonsHouse; // Just a copy of the reference
firstAddressCopy.paintDoor(Color.Red);
Color color = secondAddressCopy.getDoorColor(); // Now color will be red
Basically, remember a few rules and things will become clear:
基本上,记住一些规则,事情就会变得清晰:
- The value of an expression in Java is neveran object - only ever a reference or a primitive value
- (Corollary of first point) A variable neverholds an object - only ever a reference or a primitive value
- Assignment (and argument passing) alwayscopies the value, whether that value is a reference or a primitive value
- Java 中表达式的值永远不是对象——只有引用或原始值
- (第一点的推论)变量永远不会保存对象 - 只有引用或原始值
- 赋值(和参数传递)始终复制值,无论该值是引用还是原始值
回答by sleske
I thought Java is assigned by value, not reference?
我以为 Java 是按值分配的,而不是按引用分配的?
What does "assigned by value" mean? Are you maybe confusing it with "pass by value/reference"?
“按值分配”是什么意思?您是否可能将它与“按值/引用传递”混淆?
At any rate, if you handle a class instance in Java, you are actually handling a reference to that class (much like a pointer in C/C++). Your assignment only copies the reference, so both A and B refer to the same instance, i.e. the data is shared, hence the result.
无论如何,如果您在 Java 中处理类实例,您实际上是在处理对该类的引用(很像 C/C++ 中的指针)。你的赋值只复制引用,所以 A 和 B 都引用同一个实例,即数据是共享的,因此结果。
回答by Bozho
A is a reference to the object. So if you change the object internal state, it will be reflected to every other variable pointing to it.
A 是对对象的引用。因此,如果您更改对象内部状态,它将反映到指向它的所有其他变量。
If you re-assign A
, then B
will not change:
如果重新分配A
,则B
不会改变:
Foo a = new Foo();
Foo b = a;
a.bar = "bar"; // this is reflected in b
a = new Foo(); // b stays pointing to the previous Foo
a.bar = "baaar"; // b stays with a value of bar="bar"
(Java is passby value. Check this articleabout it.)
(Java 是按值传递的。查看这篇文章。)
回答by will
In Java, your variables can be split into two categories: Objects, and everything else (int, long, byte, etc).
在 Java 中,您的变量可以分为两类:对象和其他所有内容(int、long、byte 等)。
A primitive type (int, long, etc), holds whatever value you assign it. An object variable, by contrast, holds a reference to an object somewhere. So if you assign one object variable to another, you have copied the reference, both A and B point to the same object.
原始类型(int、long 等)保存您分配给它的任何值。相比之下,对象变量在某处保存对对象的引用。因此,如果您将一个对象变量分配给另一个对象,您就复制了引用,A 和 B 都指向同一个对象。
NOTE: Strings in Java are actually objects, not primitives, which beginners often assume.
注意:Java 中的字符串实际上是对象,而不是初学者经常假设的原语。
Hope this helps
希望这可以帮助
回答by tn2000
In java when you assign Object to Object its assign by reference. Int is non an Object in java so when you assign int to int its assign by value.
在 java 中,当您将 Object 分配给 Object 时,它是通过引用分配的。Int 不是 Java 中的对象,因此当您将 int 分配给 int 时,它是按值分配的。