Java Reference
In-Depth Information
ues that the variable can hold at run time or the expression can produce at run time. If a
run-time value is a reference that is not null , it refers to an object or array that has a class,
and that class will necessarily be compatible with the compile-time type.
Even though a variable or expression may have a compile-time type that is an interface
type, there are no instances of interfaces. A variable or expression whose type is an inter-
face type can reference any object whose class implements (§ 8.1.5 ) that interface.
Sometimes a variable or expression is said to have a “run-time type”. This refers to the
class of the object referred to by the value of the variable or expression at run time, assum-
ing that the value is not null .
The correspondence between compile-time types and run-time types is incomplete for two
reasons:
1. At run time, classes and interfaces are loaded by the Java Virtual Machine using
class loaders. Each class loader defines its own set of classes and interfaces. As a
result, it is possible for two loaders to load an identical class or interface definition
but produce distinct classes or interfaces at run time. Consequently, code that com-
piled correctly may fail at link time if the class loaders that load it are inconsistent.
See the paper Dynamic Class Loading in the Java Virtual Machine , by Sheng
Liang and Gilad Bracha, in Proceedings of OOPSLA '98 , published as ACM
SIGPLAN Notices , Volume 33, Number 10, October 1998, pages 36-44, and
The Java Virtual Machine Specification, Java SE 7 Edition for more details.
2. Type variables (§ 4.4 ) and type arguments (§ 4.5.1 ) are not reified at run time. As a
result, the same class or interface at run time represents multiple parameterized
types (§ 4.5 ) from compile-time. Specifically, all compile-time invocations of a
given generic type declaration (§ 8.1.2 , § 9.1.2 ) share a single run-time representa-
tion.
Under certain conditions, it is possible that a variable of a parameterized type
refers to an object that is not of that parameterized type. This situation is
known as heap pollution 4.12.2 ). The variable will always refer to an object
that is an instance of a class that represents the parameterized type.
Example 4.12.6-1. Type of a Variable versus Class of an Object
Click here to view code image
interface Colorable {
void setColor(byte r, byte g, byte b);
}
Search WWH ::




Custom Search