Java Reference
In-Depth Information
• The instanceof operator (§ 15.20.2 ); here the instanceof operator tests whether e
is assignment-compatible with the type ArithmeticException
4.12. Variables
A variable is a storage location and has an associated type, sometimes called its compile-
time type , that is either a primitive type (§ 4.2 ) or a reference type (§ 4.3 ).
A variable's value is changed by an assignment (§ 15.26 ) or by a prefix or postfix + + (in-
crement) or -- (decrement) operator (§ 15.14.2 , § 15.14.3 , § 15.15.1 , § 15.15.2 ) .
Compatibility of the value of a variable with its type is guaranteed by the design of the
Java programming language, as long as a program does not give rise to compile-time un-
checked warnings (§ 4.12.2 ) . Default values (§ 4.12.5 ) are compatible and all assignments
to a variable are checked for assignment compatibility (§ 5.2 ), usually at compile time, but,
in a single case involving arrays, a run-time check is made (§ 10.5 ) .
4.12.1. Variables of Primitive Type
A variable of a primitive type always holds a primitive value of that exact primitive type.
4.12.2. Variables of Reference Type
A variable of a class type T can hold a null reference or a reference to an instance of class
T or of any class that is a subclass of T .
A variable of an interface type can hold a null reference or a reference to any instance of
any class that implements the interface.
Note that a variable is not guaranteed to always refer to a subtype of its declared type,
but only to subclasses or subinterfaces of the declared type. This is due to the possib-
ility of heap pollution discussed below.
If T is a primitive type, then a variable of type “array of T ” can hold a null reference or a
reference to any array of type “array of T ”.
If T is a reference type, then a variable of type “array of T ” can hold a null reference or a
reference to any array of type “array of S ” such that type S is a subclass or subinterface of
type T .
A variable of type Object[] can hold a reference to an array of any reference type.
A variable of type Object can hold a null reference or a reference to any object, whether it is
an instance of a class or an array.
Search WWH ::




Custom Search