Java 对象a=对象b;对象a会发生什么情况?

Java 对象a=对象b;对象a会发生什么情况?,java,math,Java,Math,我的一位教授给了我们一些考试练习题,其中一个问题类似于下面的(psuedocode): 这对我来说似乎是非常基本的,所以我提出了答案a是“红色”,b是“红色”,但我被告知这是不正确的。我把我的答案分解为一道数学题: a = 15; b = 12; a = b; //a becomes 12 b = 13; b = a; //b becomes 12 但我的思维过程是通过C语言,而不是Java。我想两者都有一些通用的方法,但也许我错了?我的答案错了还是我的教授错了?我对Java非常陌生,虽然我对

我的一位教授给了我们一些考试练习题,其中一个问题类似于下面的(psuedocode):

这对我来说似乎是非常基本的,所以我提出了答案a是“红色”,b是“红色”,但我被告知这是不正确的。我把我的答案分解为一道数学题:

a = 15;
b = 12;
a = b; //a becomes 12
b = 13;
b = a; //b becomes 12

但我的思维过程是通过C语言,而不是Java。我想两者都有一些通用的方法,但也许我错了?我的答案错了还是我的教授错了?我对Java非常陌生,虽然我对C、Python和web逻辑(PHP、Ruby)有一定的了解,但如果这是一件微不足道的事情,我深表歉意。

重要的是,在
a=b
行之后,您不再有两个对象了。有两个变量,都指向同一个对象。因此,当您更改对象的颜色时,两个变量都会反映它。行
b=a
实际上没有任何作用。

a
b
是对对象的引用。当您写入
a=b
时,a被分配给先前分配给
b
的引用,因此它们现在都引用相同的对象

假设有两个对象O1和O2,它们在开始时分别被分配给
a
b

a.setColor(blue);     // the object O1 is set the color blue
b.setColor(red);      // the object O2 is set the color red
a = b;                // a now references O2, and b still references O2.
b.setColor(purple);   // the object O2 is set the color purple.
b = a;                // b is told to reference O2, which it already is.
如果你想通过C的思维来思考,你可以把
a
b
看作指针,它们可以在变量之间交换,并且可以修改其数据


对于像在C中那样处理的基本值来说,情况不同。

在Java中,将任何对象变量视为指向该对象类型值的指针通常是正确的。因此,在Java中,需要执行以下操作:

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

a.setColor(blue);
b.setColor(red);
a = b;
b.setColor(purple);
b = a;
应在C++中执行与以下内容大致相同的操作:

Color *a = new Color();
Color *b = new Color();

a->setColor(blue); // the value a points to is now blue
b->setColor(red); // the value b points to is now red
a = b; // a now points to the same value as b.
b->setColor(purple); // the value BOTH a and b point to is now purple
b = a; // this does nothing since a == b is already true
(请注意,这与参考文献不相同-根据)


还要注意,对于原语,Java变量只是值,而不是指针。因此,在基本整数的情况下,它的行为或多或少应该与您在问题中预期的一样,因为
a=b
a
的值设置为等于
b
的值,这是因为在第一个示例中,
a
b
都是对象,所以这是每个步骤中发生的情况:

       [ blue  ]     b --> [ purple ] <-- a

a假设您已经创建了两个对象,并使变量
a
b
引用它们,那么您最初会得到这样的结果

a -->  [ white ]     b --> [ white ]
你的前两行改变物体的颜色,给你

a -->  [ blue  ]     b --> [  red  ]
然后,将变量
a
指向
b
引用的对象,以便它们都引用相同的对象。你现在有

       [ blue  ]     b --> [  red  ] <-- a

最后,行
b=a
什么都不做,因为
b
已经引用了与
a

相同的对象,因为其他人已经解释了引用和原语之间的区别,我将解释这在表面下是如何工作的

所以对一个对象的引用本质上是一个数字。根据虚拟机的不同,它将是32位或64位,但仍然是一个数字。这个数字表明你在内存中的对象是。这些数字称为地址,通常以十六进制为基数编写,如so
0xyyyyyyy
(这是32位地址的一个示例,64位地址的大小是其两倍)

让我们使用上面使用32位虚拟机的示例

a.setColor(blue); // Let's assume the object pointed to by a is at 0x00000001
b.setColor(red);  // Let's assume the object pointed to by b is at 0x00000010

/* Now the following makes sense, what happens is the value of 
   a (0x00000001) is overwritten with 0x00000010. 
   This is just like you would expect if a and b were ints.
*/
a = b; // Both a and b have the value 0x00000010. They point to the same object

b.setColor(purple); // This changed the object stored at 0x00000010 

b = a; // This says nothing because both a and b already contain the value 0x00000010
这应该说明引用的工作方式与第二个示例中显示的数字类似。然而,仅在表面之下,就程序员而言,引用的赋值与原语赋值不同

在Java中,您永远不需要担心对象等的地址。在C语言和C++语言中,这使得下级访问变得更加明显和重要。p> 算术?那么你能做算术和其他你能用数字做的事情吗?简短的回答是否定的,至少在java中不是这样

<>在C和C++中,递增和递减指针到对象是完全有效的,并且使用了很多,例如遍历数组。
如果不够清楚,请随时提问。

在第一个示例中,
a
b
是对象,使用setColor方法。在第二个例子中,它们是简单的整数原语。这两个例子并不相同,因为你要处理的是苹果和橙子。@MarcB,我意识到这一点,但我认为无论使用数学还是颜色,变量初始化都应该保持一致。不管是颜色还是核打击的发射代码。分配对象和分配原语有不同的语义。为什么不简单地用java设置示例并尝试一下呢?颜色是紫色的,因为它们指向内存中的同一个对象,因此,当您更改
b
时,您同时更改
a
(修改
a
b
引用的对象。在
a=b;
之后引用的
a
对象会发生什么情况?现在无法引用它吗?它将是垃圾collected@riista:
a
可用于垃圾收集,因为上述代码段中没有变量包含的引用e> a。
a.setColor(blue); // Let's assume the object pointed to by a is at 0x00000001
b.setColor(red);  // Let's assume the object pointed to by b is at 0x00000010

/* Now the following makes sense, what happens is the value of 
   a (0x00000001) is overwritten with 0x00000010. 
   This is just like you would expect if a and b were ints.
*/
a = b; // Both a and b have the value 0x00000010. They point to the same object

b.setColor(purple); // This changed the object stored at 0x00000010 

b = a; // This says nothing because both a and b already contain the value 0x00000010