The difference between an object type and a reference type

I studied polymorphism from "Head First Java" and came to this concept. Can someone explain this with an example?

The compiler checks the class of the reference type, and not the type of the object.

So what is the difference between a reference type and an object type?

+8
java object polymorphism oop
source share
2 answers

I do not think that their use of "object type" and "reference type" is standardized, but here is my interpretation.

Consider this code:

Object o = new Integer(3); 

The o link is of type Object . The object that it refers to is of type Integer .

Thus, the "reference type" will be Object , and the "type of object" will be Integer .

What is confusing is that there is a (standardized, official) term “ link type ” that encapsulates types that can be referenced. In Java, which includes all classes, enumerations, interfaces, arrays. It excludes only primitive types ( int ..).

+27
source share

The term refers to the following:

  • object type (in your book) = actual type of referent performance
  • reference type (in your book) = static link type

It may be easier for some to understand these terms.

+6
source share

All Articles