java 对象类型和引用类型的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16730109/
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
Difference Between Object Type and Reference Type
提问by Sikander
I was studying Polymorphism from "Head First Java" and came to this concept. Can anyone explain it please with an example?
我正在从“Head First Java”学习多态并得出这个概念。任何人都可以用一个例子解释一下吗?
Compiler checks the class of reference type -- not the Object type.
编译器检查引用类型的类——而不是对象类型。
So what's the difference between Reference Type and Object Type?
那么引用类型和对象类型有什么区别呢?
回答by Joachim Sauer
I don't think their use of "object type" and "reference type" is standardized, but here's my interpretation.
我不认为他们对“对象类型”和“引用类型”的使用是标准化的,但这是我的解释。
Consider this code:
考虑这个代码:
Object o = new Integer(3);
The referenceo
is of type Object
. The objectthat it references is of type Integer
.
所述参考o
是类型Object
。它引用的对象的类型是Integer
。
So the "reference type" would be Object
and the "object type" would be Integer
.
所以“引用类型”将是Object
,“对象类型”将是Integer
.
What makes this confusing is that there's the (standardized, official) term "reference type" that encapsulates types that can be referenced. In Java that includes all classes, enums, interfaces, arrays. It excludes only the primitive types (int
, ...).
令人困惑的是,有一个(标准化的、官方的)术语“引用类型”封装了可以引用的类型。在 Java 中,包括所有类、枚举、接口、数组。它仅排除原始类型 ( int
, ...)。
回答by Bruno Reis
What is meant by the terms is the following:
这些术语的含义如下:
- object type(in your book) = the actual runtime type of the referent
- reference type(in your book) = the static type of the reference
- 对象类型(在您的书中)= 所指对象的实际运行时类型
- 引用类型(在您的书中)= 引用的静态类型
Maybe some will find it easier to understand with these terms.
也许有些人会发现使用这些术语更容易理解。