Java Generics

New feature of Java 1.5 which allows abstraction over types. Syntactically similar to templates in CeePlusPlus but quite different in semantics. They're similar in that they both use that horrid angle-bracket syntax that makes it easy to confuse nested template declarations with a right-shift, if you don't put a space between the closing brackets...

What syntax would you have preferred?

Umm, Java corrects the angle bracket problem. You can write >> or >>> without spaces in a generic, and not have it interpreted as a right shift.
Unfortunately, JavaGenerics are too complex for the value they provide. The primary problem is TypeErasure; a generic class in Java represents a single type, regardless of the parameters. That is, Foo<Long> and Foo<String> are the same type. This makes it impossible to use generics to implement JamesCoplien's CuriouslyRecurringTemplate idiom, for example. Despite their name, JavaGenerics do not support GenericProgramming.

JavaGenerics are basically worthless except as a means of eliminating the need to do some casting when using collections.
Links:

See also GenericJava.
CategoryJava

EditText of this page (last edited May 3, 2006) or FindPage with title or text search