简体   繁体   English

传递对象参考变量

[英]Passing object reference variables

This is from the famous SCJP Java book.My question is how does dim get the value of 11 here. 这是来自著名的SCJP Java书。我的问题是dim在这里如何获得11的值。

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. 当我们运行此类时,我们可以看到modify()方法确实能够修改在第4行创建的原始(唯一)Dimension对象。

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. 请注意,当第4行上的Dimension对象传递给Modify()方法时,该方法内部发生的对该对象的任何更改都将传递给其引用已传递的对象。 In the preceding example, reference variables d and dim both point to the same object. 在前面的示例中,参考变量d和dim都指向同一对象。

My question is how does dim get the value of 11 here 我的问题是暗淡如何在这里获得11的值

dim doesn't . dim 没有 dim.height does. dim.height可以。 When the code passes dim into the method, the value passed in is a reference to an object. 当代码将dim传递给方法时,传递的值是对对象的引用。 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: 因此,当我们将其传递给modify ,在调用modify ,我们有:

+-------------+
|      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. 现在, ddim每个都有一个值的副本,该值告诉JVM对象在内存中的位置。

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. 就像JVM可以用来在内存中其他位置查找对象的数字一样。

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. ...但是变量/数据成员/参数不包含对象,它们包含引用对象的值。

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

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