Java Reference
In-Depth Information
the list
Object item;
// The item for this ListItem
}
}
The type parameter following the class name in the original generic type definition has been removed,
and all occurrences of the type variable
Point
within the class definition have been replaced by type
Object
. The bold lines are the one affected. Type
Object
is chosen by the compiler to replace the type
variable because type
Object
is the ultimate superclass class from which type
Point
is derived. The type
that the compiler selects to replace a type variable is the
leftmostbound
of the type variable. Type
Object
is the default leftmost bound that applies to any class type because all classes have type
Object
as their
ultimate superclass. You see later in the chapter how you can specify a different leftmost bound for a type
parameter and what the reasons are for doing this.
Of course, this class doesn't exist as a separate entity. The preceding code represents a description of how
the generic type behaves when you supply a type argument as type
Point
. Looking at the class that now
works with references of type
Object
, you may wonder what the advantage of being able to specify the
type parameter is; after all, you can supply a reference to an object of any type for a parameter of type
Object
. The answer is that the type variable you supply is used by the compiler to ensure compile-time
type safety. When you use an object of type
LinkedList<Point>
in your code, the compiler checks that
you use it only to store objects of type
Point
and flags any attempt to store objects of other types as
an error. When you call methods for an object of type
LinkedList<Point>
, the compiler ensures that
you supply references only of type
Point
where the original method parameter was specified as the type
parameter.
You use the methods in the parameterized type in the same way as those in the original
LinkedList
class.
Everything is very straightforward, and you now have a
PolyLine
implementation that uses a type safe
linked list. I think you'll agree that the essentials of defining and using a generic type could be described
as a piece of cake.
Using Primitive Type Wrapper Class Types as Arguments
On occasion you will want to store values of a primitive type in a collection such as a linked list. In this situ-
ation you use the generic type with one of the wrapper classes for primitive types as the type argument —
these are the classes
Integer
,
Short
,
Double
, and so on that are defined in the
java.lang
package. Here's
how you could use the
LinkedList<T>
generic type to hold values of type
double
:
LinkedList<Double> temperatures = new LinkedList<>();
Here you have created a linked list that stores objects of type
Double
, and the autoboxing facility that you
met in Chapter 5 enables you to use the
LinkedList<Double>
object directly with values of type
double
.
For example, to add a value of type
double
to the linked list you have just created, you could write the fol-
lowing:
temperatures.addItem(10.5);
Because the parameter type for this method for the
LinkedList<Double>
object is of type
Double
, the
compiler automatically inserts a boxing conversion to convert the
double
value 10.5 to an object of type