简体   繁体   English

Java中的引用变量里面有什么?

[英]What's inside a reference variable in Java?

We know that the object reference variable holds bits that represent a way to access an object. 我们知道对象引用变量包含表示访问对象的方式的位。

It doesn't hold the object itself, but it holds something like a pointer or an address. 它不包含对象本身,但它包含指针或地址之类的东西。

I was going through the Head-First Java (2nd edition) book and in the book it is written (in Chapter 3, page 54) that 我正在阅读Head-First Java(第2版)的书,并在书中写了(在第3章,第54页)

In Java we don't really know what is inside a reference variable. 在Java中,我们并不真正知道引用变量中的内容。 We do know that whatever it is, it represents one and only one object. 我们确实知道无论它是什么,它代表着唯一的一个对象。 And the JVM knows how to use the reference to get to the object. 并且JVM知道如何使用引用来获取对象。 - -

I want to ask: 我想问问:

  • Do an object reference variable holds pointer, address or what? 对象引用变量是否包含指针,地址或什么?
  • How does JVM interpret that? JVM如何解释这一点?

It's entirely up to the JVM to determine what goes inside a reference. 完全取决于JVM来确定引用内部的内容。

In the simplest case it would just be a pointer (ie an address). 在最简单的情况下,它只是一个指针(即一个地址)。 In more sophisticated cases, the VM may use different representations depending on the situation - for example, you may want to read the paper on "Compressed oops in HotSpot" to see how the HotSpot VM can avoid doubling the size of references in some (but not all) places when running as a 64-bit VM. 在更复杂的情况下,VM可能会根据具体情况使用不同的表示形式 - 例如,您可能希望阅读有关“HotSpot中的压缩oops”的文章,以了解HotSpot VM如何避免在某些情况下将引用的大小加倍(但是并非全部)作为64位VM运行时的位置。

The important thing is that you should neither know nor care. 重要的是你既不知道也不关心。 As far as you're concerned as a programmer, it's just an opaque set of bits - its only purpose is to let you get to the object in question (or identify a null reference). 就你作为程序员而言,它只是一组不透明的位 - 它的唯一目的是让你到达有问题的对象(或识别一个空引用)。

That's up to the JVM. 这取决于JVM。 A Java reference isn't guaranteed to have any semantics besides that you can access an object through it. 除了可以通过它访问对象之外,不保证Java引用具有任何语义。 Sunacle might do that differently to IBM as well. Sunacle也可能与IBM有所不同。

In practice it may often be a pointer of some sort, though maybe not directly to the object since that can be moved by the GC. 在实践中,它可能经常是某种指针,但可能不是直接指向对象,因为它可以由GC移动。

It's entirely JVM specific. 它完全是JVM特有的。 It may be an address, a pointer, or something more complicated. 它可能是地址,指针或更复杂的东西。 You don't have, or need, any harder guarantees than the fact that you can get an Object using the reference. 除了可以使用引用获取Object之外,您没有或不需要任何更难的保证。

It's up to the JVM to decide exactly how the reference is implemented. 由JVM决定如何实现引用。

Having said that, any "sane" implementation would probably use either a direct pointer or some form of compressed pointer for performance reasons. 话虽如此,出于性能原因,任何“理智”的实现都可能使用直接指针或某种形式的压缩指针。 I believe this applies to all current production JVMs. 我相信这适用于所有当前生产的JVM。

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

相关问题 可以在 Java 中的类中初始化变量吗? 静态变量呢? 如果是,那么静态块有什么用? - Can a Variable be initialized inside a Class in Java? What about Static Variable? If yes, then what's the use for Static Block? 从方法内部访问参考变量 - Accesing a reference variable from inside a method java 什么是未初始化的引用变量,它不为null - What is a uninitialized reference variable when it's not null 在 Java 中,object 变量引用指向什么? - In Java, what does object variable reference point to? Java中的指针和引用变量有什么区别? - What is the difference between a pointer and a reference variable in Java? Java中的Object,Reference ID和Reference Variable有什么区别? - What is the difference between an Object , Reference ID , and Reference Variable in Java? Java引用和C ++引用之间有什么区别 - What's the difference between a Java reference and C++ reference Java变量未在Javascript的setinterval中更新 - Java variable not updating inside Javascript's setinterval 编译后Java对象引用变量类型会发生什么? - What happens to Java object reference variable types after compilation? 在Java中,引用子类引用的超类变量的实际类是什么? - In Java, what is the actual class for a superclass variable that is referencing a subclass reference?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM