Java Reference
In-Depth Information
Characters in strings can be specified with the octal digit syntax, but
all three octal digits should be used to prevent accidents when an octal
value is specified next to a valid octal digit in the string. For example,
the string
"\0116"
is equivalent to
"\t6"
, whereas the string
"\116"
is
equivalent to
"N"
.
7.2.7. Class Literals
Every type (primitive or reference) has an associated instance of class
Class
that represents that type. These instances are often referred to as
the
class object
for a given type. You can name the class object for a
type directly by following the type name with
".class"
, as in
String.class
java.lang.String.class
java.util.Iterator.class
boolean.class
The first two of these class literals refer to the same instance of class
Class
because
String
and
java.lang.String
are two different names for
the same type. The third class literal is a reference to the
Class
instance
instance that represents the primitive type
boolean
.
Since class
Class
is generic, the actual type of the class literal for a ref-
erence type
T
is
Class<T>
, while for primitive types it is
Class<W>
where
W
is the wrapper class for that primitive type. But note, for example,
that
boolean.class
and
Boolean.class
are two different objects of type
Class<Boolean>
. Generic types are discussed in
Chapter 11
,
and the class
Class
is discussed in
Chapter 16
.
Exercise 7.2
: Write a class that declares a field for each of the primitive
numeric types, and try to assign values using the different literal forms-
for example, try to assign
3.5f
to an
int
field. Which literals can be used