I've been stumbling over Java's generic types for a while now... I think I finally understand what my problem is:
They should be called specific types, or parametrised-collections!
I think the feature is called "Java generics" because when you use a collection, say List<E>
, that List
contains elements of parameterised type <E>
, a "generic type". But the element type in the plain old-fashioned non-parametrised collection List
is also generic! In fact the non-parametrised List
is more generic than the "generic" version (it can contain elements of any sub-class of Object
)! The distinction is that the elements in a List<String>
are all of one specific type (String
), whereas the elements in a List
can be of any general type, and can even be of different types in the same List
collection (leading to problems that "Java generics" was invented to address).
<sarcasm>
Gosh, what an epiphany.</sarcasm>
If you're a Java guru this is nothing new, of course. But that took me ages to grasp. I think I wasn't helped by the terminology. In fact whenever I'm reading Java literature, it helps if I swap "generic" for "specific". This makes much more sense to me. Actually I already do word substitutions for much of the Java canon. For instance: