Java 5.0 added generic types and methods to the language, collectively
known as Java Generics. Generics are a widely debated new language
feature, mainly because they are amazingly complex, at least more complex
as most people had expected.
The key concepts of Java Generics are easy to grasp: parameterize a
type with a type variable, et voilà, the result is a generic type.
Likewise for generic methods. An example of a generic type is the generic
interface
List<E> {...}
which can be used to form parameterized
types such as
List<String>
or
List<Double>
, but
also
List<? extends Number>
, which involves use of a wildcard.
Wildcards are probably the most difficult-to-grasp element of Java Generics.
Yet, they are everything but syntactic sugar. Quite the converse,
they are frequently needed as argument types of carefully crafted methods.
An example is
<T> void sort(List<T> list,Comparator<? super
T> c)
in class
java.util.Collections
. The "
? super
T
"-wildcard is needed, because the method would otherwise mistakenly
prevent sorting a
List<Double>
with a
Comparator<Number>
.
In this session we will give a brief recap of the key concepts of Java
Generics and all involved syntax elements. Then we, as a mob, will program
using Java generics. The idea is the following: the presenter sets
up a programming task, the attendants suggest the source code, the presenter
types it in, and then we'll see what happens. It's a unique opportunity
to learn everything you always wanted to know about generics (but were
afraid to ask) and to step into every known pitfall (that a novice to generics
typically steps into). |