Java Reference
In-Depth Information
Section 20.4 Additional Compile-Time Translation Issues: Methods That Use a Type
Parameter as the Return Type
• When a generic method is compiled, the compiler performs erasure (p. 848) to remove the type-
parameter section and replace the type parameters with actual types. By default each type param-
eter is replaced with its upper bound (p. 848), which is Object unless specified otherwise.
• When erasure is performed on a method that returns a type variable (p. 848), explicit casts are
inserted in front of each method call to ensure that the returned value has the type expected by
the caller.
Section 20.5 Overloading Generic Methods
• A generic method may be overloaded with other generic methods or with nongeneric methods.
Section 20.6 Generic Classes
• Generic classes provide a means for describing a class in a type-independent manner. We can
then instantiate type-specific objects of the generic class.
• A generic class declaration looks like a nongeneric class declaration, except that the class name is
followed by a type-parameter section. The type-parameter section of a generic class can have one
or more type parameters separated by commas.
• When a generic class is compiled, the compiler performs erasure on the class's type parameters
and replaces them with their upper bounds.
• Type parameters cannot be used in a class's static declarations.
• When instantiating an object of a generic class, the types specified in angle brackets after the class
name are known as type arguments (p. 851). The compiler uses them to replace the type param-
eters so that it can perform type checking and insert cast operations as necessary.
Section 20.7 Raw Types
• It's possible to instantiate a generic class without specifying a type argument. In this case, the new
object of the class is said to have a raw type (p. 857)—the compiler implicitly uses type Object
(or the type parameter's upper bound) throughout the generic class for each type argument.
Section 20.8 Wildcards in Methods That Accept Type Parameters
• Class Number is the superclass of both Integer and Double .
Number method doubleValue (p. 862) obtains the Number 's underlying primitive value as a double
value.
• Wildcard type arguments enable you to specify method parameters, return values, variables, and
so on, that act as supertypes of parameterized types. A wildcard-type argument is denoted by ?
(p. 862), which represents an “unknown type.” A wildcard can also have an upper bound.
• Because a wildcard ( ? ) is not a type-parameter name, you cannot use it as a type name throughout
a method's body.
• If a wildcard is specified without an upper bound, then only the methods of type Object can be
invoked on values of the wildcard type.
• Methods that use wildcards as type arguments (p. 862) cannot be used to add elements to a col-
lection referenced by the parameter.
Self-Review Exercises
20.1
State whether each of the following is true or false . If false , explain why.
a)
A generic method cannot have the same method name as a nongeneric method.
Search WWH ::




Custom Search