当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Passing object reference variables

问题描述:

This is from the famous SCJP Java book.My question is how does dim get the value of 11 here.

import java.awt.Dimension;

class ReferenceTest {

public static void main (String [] args) {

Dimension d = new Dimension(5,10);

ReferenceTest rt = new ReferenceTest();

System.out.println("Before modify() d.height = "+ d.height);

rt.modify(d);

System.out.println("After modify() d.height = "+ d.height);

}

void modify(Dimension dim) {

dim.height = dim.height + 1;

System.out.println("dim = " + dim.height);

}

}

When we run this class, we can see that the modify() method was indeed able to modify the original (and only) Dimension object created on line 4.

C:\Java Projects\Reference>java ReferenceTest

Before modify() d.height = 10

dim = 11

After modify() d.height = 11

Notice when the Dimension object on line 4 is passed to the modify() method, any changes to the object that occur inside the method are being made to the object whose reference was passed. In the preceding example, reference variables d and dim both point to the same object.

网友答案:

My question is how does dim get the value of 11 here

dim doesn't. dim.height does. When the code passes dim into the method, the value passed in is a reference to an object. The method then modifies the state of that object (which doesn't modify the reference at all), and so the calling code sees the updated state.

This code:

Dimension d = new Dimension(5, 10);

Produces something like this in memory:

+-------------+
|      d      |
+-------------+     +--------------------+
| (reference) |---->| Dimension instance |
+-------------+     +--------------------+
                    | width: 5           |
                    | height: 10         |
                    +--------------------+

The variable holds a value that refers to the object elsewhere in memory. You can copy that value (called an object reference), for instance by passing it into a method, but it still refers to the same object.

So when we pass that into modify, during the call to modify, we have:

+-------------+
|      d      |
+-------------+
| (reference) |--+
+-------------+  |
                 |  +--------------------+
                 +->| Dimension instance |
                 |  +--------------------+
                 |  | width: 5           |
                 |  | height: 10         |
                 |  +--------------------+
+-------------+  |
|     dim     |  |
+-------------+  |
| (reference) |--+
+-------------+

Now, d and dim each have a copy of the value that tells the JVM where the object is in memory.

This is fundamental to how objects work: The value held by variables, data members, and arguments is a reference to the object, not a copy of it. It's like a number the JVM can use to look up the object elsewhere in memory.

Primitives are actually held inside the variable/data member/argument:

int n = 10;

Gives us:

+----+
| n  |
+----+
| 10 |
+----+

...but variables/data members/arguments don't hold objects, they hold a value that refers to an object.

分享给朋友:
您可能感兴趣的文章:
随机阅读: