Assigning one primitive variable to another vs a reference variable to another

烂漫一生 提交于 2019-11-29 15:53:06
Makoto

This is the underpinning difference between a reference and a primitive. In both cases, you're getting the actual value, but only in the case of an object do you have a chance to impact the result of any other usages of it.

Let's walk through the code.

int a = 10;
int b = a;

These two declarations are saying the following:

  • Assign the value 10 to an int identifier called a.
  • Assign the value of a to an int identifier called b.

So far, so good. Nowhere do we say that a is referenced by b; we're only taking values.

If we declare:

b = 30;

We're saying take the value of 30 and assign it to the identifier b.

We don't do anything with a at that point; it already contains the value 10. This is why a and b are different.

Now, when we get to the object, things don't really change on the surface...

Dimension a1 = new Dimension(5, 10);
Dimension b1 = a1;

We translate this as:

  • Assign the value of instantiation of a new Dimension with (int) parameters 5 and 10 to a Dimension identifier a1.
  • Assign the value of a1 to a Dimension identifier b1.

We're still assigning values here, since Java is pass-by-value. The kicker here is that in Java, the value of an object is still a reference to that object.

By the above example, a1 and b1 are pointing to the same instance.

Now, when we state this:

b1.height = 30;

We're actually saying:

  • Assign the value 30 to the field height dereferenced through the value b1.

We're still referring to the value of b1 here, which is tied to a1. This is why you see the difference; because a1 and b1 refer to the same value (which is the same reference), any change done through the identifier b1 is reflected through a1.

There is only one instance

When you say

Dimension b1= a1; // <-- assigns reference of a1 to b1.

you assign the reference address that a1 refers to to b1. Thus, when you modify the height field through b1 you also modify a1.

 b1.height=30; // <-- a1.height = 30

Create another instance

If you want b1 to be a unique referene, then you use new.

 Dimension b1= new Dimension(a1.width, a1.height); // <-- creates a new Dimension
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!