对象类型和引用类型之间的区别

问题描述:

我正在研究"Head First Java"中的多态性,并提出了这个概念.有人可以举例说明吗?

I was studying Polymorphism from "Head First Java" and came to this concept. Can anyone explain it please with an example?

编译器会检查引用类型的类,而不是对象类型.

Compiler checks the class of reference type -- not the Object type.

那么引用类型和对象类型之间有什么区别?

So what's the difference between Reference Type and Object Type?

我认为它们对对象类型"和引用类型"的使用不是标准化的,但这是我的解释.

I don't think their use of "object type" and "reference type" is standardized, but here's my interpretation.

考虑以下代码:

Object o = new Integer(3);

引用 o的类型为Object.它引用的 object 类型为Integer.

The reference o is of type Object. The object that it references is of type Integer.

因此,引用类型"将为Object,对象类型"将为Integer.

So the "reference type" would be Object and the "object type" would be Integer.

造成混淆的是,(标准化的,正式的)术语"引用类型"封装了可以被引用.在Java中,它包括所有类,枚举,接口,数组.它仅排除基本类型(int,...).

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, ...).