![](/img/trans.png)
[英]If an array is assigned to an Object Reference, then what does the Object Reference point to?
[英]In Java, what does object variable reference point to?
我看到一个数组引用指向数组 [0] 的 memory 地址。 object 变量的 memory 地址指向什么? 即 Car c1 = new Car("Chevy", "Tahoe");
以为它可能指向第一个实例变量,但这没有意义 b/c 可以有没有实例变量的类。
object 变量的 memory 地址指向什么?
object 只是 memory 的一部分。 在 Java 的典型实现中,该块是更大范围的 memory 的一部分,称为堆。
究竟是什么构成了 object 的结构,它的特定位和字节与我们普通的 Java 程序员无关。 object 的结构如何在 memory 中布局是我们不关心的实现细节。
即 Car c1 = new Car("Chevy", "Tahoe");
这里涉及两个步骤。
赋值右侧=
实例化 object。 这意味着在堆中找到了一块 memory。 传递给构造函数的值被写入 memory 中的成员字段。
赋值的左侧=
获取新创建的 object 的位置,并将其分配给名为c1
的变量。 如果c1
是一个局部变量,它位于堆栈中,在 Java 的典型实现中。
在 Java 中,我们从未看到实际的 memory 地址。 In code such as c1.model
, we trust the JVM will find the c1
var on the stack, use its internally-maintained memory address to access the particular Car
object, then navigate within that object's allocated chunk of memory to retrieve the value of the名为model
的成员字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.