Java Reference
In-Depth Information
Generics enable you to detect errors at compile time rather than at runtime.
Key
Point
You have used a generic class
ArrayList
in Chapter 11 and generic interface
Comparable
in Chapter 13.
Generics
let you parameterize types. With this capability, you can define a
class or a method with generic types that the compiler can replace with concrete types. For
example, Java defines a generic
ArrayList
class for storing the elements of a generic type.
From this generic class, you can create an
ArrayList
object for holding strings and an
ArrayList
object for holding numbers. Here, strings and numbers are concrete types that
replace the generic type.
The key benefit of generics is to enable errors to be detected at compile time rather than
at runtime. A generic class or method permits you to specify allowable types of objects that
the class or method can work with. If you attempt to use an incompatible object, the compiler
will detect that error.
This chapter explains how to define and use generic classes, interfaces, and methods and
demonstrates how generics can be used to improve software reliability and readability. It can
be intertwined with Chapter 13, Abstract Classes and Interfaces.
what is generics?
why generics?
The motivation for using Java generics is to detect errors at compile time.
Key
Point
Java has allowed you to define generic classes, interfaces, and methods since JDK 1.5. Several
interfaces and classes in the Java API were modified using generics. For example, prior to
JDK 1.5 the
java.lang.Comparable
interface was defined as shown in Figure 19.1a, but
since JDK 1.5 it is modified as shown in Figure 19.1b.
package
java.lang;
package
java.lang;
public interface
Comparable {
public int
compareTo(Object o)
}
public interface
Comparable<T> {
public int
compareTo(T o)
}
(a) Prior to JDK 1.5
(b) JDK 1.5
F
IGURE
19.1
The
java.lang.Comparable
interface was modified in JDK 1.5 with a
generic type
Here,
<T>
represents a
formal generic type
, which can be replaced later with an
actual
concrete type
. Replacing a generic type is called a
generic instantiation
. By convention, a
single capital letter such as
E
or
T
is used to denote a formal generic type.
To see the benefits of using generics, let us examine the code in Figure 19.2. The statement
in Figure 19.2a declares that
c
is a reference variable whose type is
Comparable
and invokes
the
compareTo
method to compare a
Date
object with a string. The code compiles fine, but
it has a runtime error because a string cannot be compared with a date.
formal generic type
actual concrete type
generic instantiation
Comparable c =
new
Date();
System.out.println(c.compareTo(
"red"
));
Comparable<Date> c =
new
Date();
System.out.println(c.compareTo(
"red"
));
(a) Prior to JDK 1.5
(b) JDK 1.5
F
IGURE
19.2
The new generic type detects possible errors at compile time.
The statement in Figure 19.2b declares that
c
is a reference variable whose type is
Comparable<Date>
and invokes the
compareTo
method to compare a
Date
object with a
string. This code generates a compile error, because the argument passed to the
compareTo
Search WWH ::
Custom Search