Java Reference
In-Depth Information
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-
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-
to a variable are checked for assignment compatibility (§
5.2
), usually at compile time, but,
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.