Generics in the Java Programming Language (JDK)
Posted in Programming Sponsored LinksJDK1.5 introduces several extensions to the Java programming Language.One of these
is the introduction of generics.
Thistutorialisaimedatintroducingyou to generics.Youmaybefamiliarwith
similarconstructs from other languages, mostnotablyC++templates.Ifso,you’llsoon
seethatthere are bothsimilaritiesandimportantdifferences.Ifyouarenotfamiliar
withlook-a-alikeconstructsfromelsewhere,allthebetter;youcanstartafresh,without
unlearninganymisconceptions.
Genericsallowyoutoabstractovertypes. The most commonexamplesarecon-
tainertypes,suchasthoseintheCollectionhierarchy.
Hereisatypicalusageofthatsor
Contents
1Introduction2
2De?ningSimpleGenerics3
3GenericsandSubtyping4
4Wildcards5
4.1BoundedWildcards………………………6
5GenericMethods7
6InteroperatingwithLegacyCode10
6.1UsingLegacyCodeinGenericCode………………10
6.2ErasureandTranslation…………………….12
6.3UsingGenericCodeinLegacyCode………………13
7TheFinePrint14
7.1AGenericClassisSharedbyallitsInvocations…………14
7.2CastsandInstanceOf……………………..14
7.3Arrays…………………………….15
8ClassLiteralsasRun-timeTypeTokens16
9MoreFunwithWildcards18
9.1WildcardCapture……………………….20
10ConvertingLegacyCodetoUseGenerics20
11Acknowledgements23
Sponsored Links
Users
