THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

its element type followed by []. Local inner classes and anonymous inner classes do not have canonical
names. You can get the canonical name of a type from the Class method getCanonicalName.


The canonical name is an example of a fully qualified namethe simple name qualified by the package and
enclosing type, if applicable. While simple names might be ambiguous in a given context, a fully qualified
name uniquely determines a type. Each type has only one canonical name, but nested types (and so arrays of
nested types) can have multiple fully qualified names. For example, the java.util.SortedMap interface
extends java.util.Map and inherits the nested Entry interface. Consequently, you can identify the
Entry type by the fully qualified name java.util.SortedMap.Entry.


The binary name of a class or interface (not array) is a name used to communicate with the virtual machine,
such as by passing it to Class.forName to ask the virtual machine for a specific Class object. The binary
name of a top-level class or interface is its canonical name. For nested types there is a special naming
convention, as you learned in "Implementation of Nested Types" on page 149. Static nested types and inner
classes (excluding local and anonymous inner classes) have a binary name that consists of the binary name of
their immediately enclosing type, followed by $ and the simple name of the nested or inner type. For
example, the binary name of the Entry interface is java.util.Map$Entry. For local inner classes, the
binary name is the binary name of the enclosing class, followed by $, then a number, and then its simple
name. For an anonymous inner class, the binary name is the binary name of the enclosing class, followed by $
and a number.[3] You get the binary name of a type from the Class method getName. This binary name is
what forName expects as a class or interface name.


[3] Since the binary name is not uniquely specified for local and anonymous classes, you
cannot use reflection to instantiate these classes in a portable way. Fortunately, it is
exceedingly rare that you would want to.

For their names, array types have a special notation, known as internal format because it is the format used
inside the virtual machine. But for some reason this notation is not considered a binary name. This notation
consists of a code representing the component type of the array, preceded by the character [. The component
types are encoded as follows:


B byte

C char

D double

F float

I int

J long

Lclassname; class or interface

S short

Z boolean

For example, an array of int is named [I, while an array of Object is named [Ljava.lang.Object;
(note the trailing semicolon). A multidimensional array is just an array whose component type is an array, so,
for example, a type declared as int[][] is named [[Ian array whose component type is named [I. These
internal format names can be passed to forName to obtain the class objects for array types, and it is this
name that getName returns for array types.

Free download pdf