Java 什么是对象引用变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16504084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:11:17  来源:igfitidea点击:

What is Object Reference Variable?

java

提问by user2301829

What is Object Reference variable in java?

java中的对象引用变量是什么?

Does the reference variable holds the memory address of the object?

引用变量是否保存了对象的内存地址?

I am confused. Please do explain.

我很迷惑。请解释一下。

采纳答案by MadProgrammer

I'm not sure I have the elegance to properly answer this, but...

我不确定我是否有能力正确回答这个问题,但是......

  • An Object is an instance of a Class, it is stored some where in memory
  • A reference is what is used to describe the pointer to the memory location where the Object resides.
  • A variable is a means by which you can access that memory location within your application (its value is "variable"). While a variable can only point to a single memory address (if its not null), it may change and point to different locations through out the life cycle of the application
  • 对象是类的实例,它存储在内存中的某个位置
  • 引用用于描述指向对象所在内存位置的指针。
  • 变量是您可以在应用程序中访问该内存位置的一种方式(它的值是“变量”)。虽然变量只能指向单个内存地址(如果它不为空),但它可能会在应用程序的整个生命周期中更改并指向不同的位置

回答by Sanjaya Liyanage

yes Object reference is the variable that holds the memory location of the real object

是 对象引用是保存真实对象内存位置的变量

回答by Niklas

In Java all objects are referred to by references for instance

在 Java 中,所有对象都通过引用来引用,例如

Object o = "foo";

Object o = "foo";

The above example has a reference, o, to the object "foo".

上面的例子有一个对对象“foo”的引用o。

回答by Stephen C

What is Object Reference variable in java?

java中的对象引用变量是什么?

Simply, it is a variable whose type is an object type; i.e. some type that is either java.lang.Objector a subtype of java.lang.Object.

简单来说,它是一个类型为对象类型的变量;即一些类型的或者是java.lang.Object或的子类型java.lang.Object

Does the reference variable hold the memory address of the object?

引用变量是否持有对象的内存地址?

Probably yes, but possibly no.

可能是,但也可能不是。

It depends on how the JVM represents object references. In most JVMs, the object reference isrepresented behind the scenes using a memory address or pointer. But it couldalso be represented as an index into an array ... or something else. (Indeed, I've messed around with an experimental JVM where an object reference was actually an index into an array of pointers.)

这取决于 JVM 如何表示对象引用。在大多数JVM中,对象引用使用的存储器地址或指针幕后表示。但它可能也可以表示为一个索引到一个数组...或别的东西。(事实上​​,我搞砸了一个实验性的 JVM,其中对象引用实际上是指向指针数组的索引。)

The point is that Java object references are an abstraction that is designedto hidethe representation / implementation details from you. The actual representation should not concern you ... since it doesn't matter if you program in pure Java. You can't get hold of the actual memory address in pure Java ... and that's a good thing. The JVM (specifically the garbage collector) is liable to change an object's actual memory address without telling you. If an application could obtain and use object addresses, it would need to deal with that, and it is a fundamentally difficult problem.

关键是 Java 对象引用是一种抽象,旨在向您隐藏表示/实现细节。实际表示不应该与您有关......因为您是否使用纯 Java 编程并不重要。你不能在纯 Java 中获得实际的内存地址......这是一件好事。JVM(特别是垃圾收集器)可能会在不告诉您的情况下更改对象的实际内存地址。如果应用程序可以获取和使用对象地址,它就需要处理它,这是一个根本性的难题。

回答by gifpif

Object Reference variable is just like pointer in c but not exactly a pointer.
Its depend's upon JRE provide some JRE treated just like a pointer and some other JRE treated as pointer to pointer.
so refernce variable just define a way to reach your object. Java is platform independent language so memory management is different in different devices so its difficult to give a unique way to reach the object.

对象引用变量就像 c 中的指针,但不完全是指针。
它依赖于 JRE 提供一些 JRE 被视为一个指针,而其他一些 JRE 被视为指向指针的指针。
所以refernce variable just define a way to reach your object。Java 是独立于平台的语言,因此不同设备的内存管理是不同的,因此很难给出一种独特的方式来访问对象。