Generics in the Java Programming LanguageGilad BrachaJuly 5, 2004Contents1 Introduction 22 Defining Simple Generics 33 Generics and Subtyping 44 Wildcards 54.1 Bounded Wildcards . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Generic Methods 76 Interoperating with Legacy Code 106.1 Using Legacy Code in Generic Code . . . . . . . . . . . . . . . . . . 106.2 Erasure and Translation . . . . . . . . . . . . . . . . . . . . . . . . . 126.3 Using Generic Code in Legacy Code . . . . . . . . . . . . . . . . . . 137 The Fine Print 147.1 A Generic Class is Shared by all its Invocations . . . . . . . . . . . . 147.2 Casts and InstanceOf . . . . . . . . . . . . . . . . . . . . . . . . . . 147.3 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Class Literals as Run time Type Tokens 169 More Fun with Wildcards 189.1 Wildcard Capture . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2010 Converting Legacy Code to Use Generics 2011 Acknowledgements 2311 IntroductionJDK 1.5 introduces several extensions to the Java programming language. One of theseis the introduction of generics.This tutorial is aimed at introducing you to generics. You may be familiar withsimilar constructs from other languages, most notably C++ templates. If so, you’ll soonsee that there are both similarities and important differences. If you are not familiarwith look a alike constructs from elsewhere, all the better; you can start afresh, ...
Voir