generics-tutorial - Generics in the Java Programming...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Generics in the Java Programming Language Gilad Bracha July 5, 2004 Contents 1 Introduction 2 2 Defining Simple Generics 3 3 Generics and Subtyping 4 4 Wildcards 5 4.1 Bounded Wildcards . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 5 Generic Methods 7 6 Interoperating with Legacy Code 10 6.1 Using Legacy Code in Generic Code . . . . . . . . . . . . . . . . . . 10 6.2 Erasure and Translation . . . . . . . . . . . . . . . . . . . . . . . . . 12 6.3 Using Generic Code in Legacy Code . . . . . . . . . . . . . . . . . . 13 7 The Fine Print 14 7.1 A Generic Class is Shared by all its Invocations . . . . . . . . . . . . 14 7.2 Casts and InstanceOf . . . . . . . . . . . . . . . . . . . . . . . . . . 14 7.3 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 8 Class Literals as Run-time Type Tokens 16 9 More Fun with Wildcards 18 9.1 Wildcard Capture . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 10 Converting Legacy Code to Use Generics 20 11 Acknowledgements 23 1 1 Introduction JDK 1.5 introduces several extensions to the Java programming language. One of these is the introduction of generics . This tutorial is aimed at introducing you to generics. You may be familiar with similar constructs from other languages, most notably C++ templates. If so, youll soon see that there are both similarities and important differences. If you are not familiar with look-a-alike constructs from elsewhere, all the better; you can start afresh, without unlearning any misconceptions. Generics allow you to abstract over types. The most common examples are con- tainer types, such as those in the Collection hierarchy. Here is a typical usage of that sort: List myIntList = new LinkedList(); // 1 myIntList.add(new Integer(0)); // 2 Integer x = (Integer) myIntList.iterator().next(); // 3 The cast on line 3 is slightly annoying. Typically, the programmer knows what kind of data has been placed into a particular list. However, the cast is essential. The compiler can only guarantee that an Object will be returned by the iterator. To ensure the assignment to a variable of type Integer is type safe, the cast is required. Of course, the cast not only introduces clutter. It also introduces the possibility of a run time error, since the programmer might be mistaken. What if programmers could actually express their intent, and mark a list as being restricted to contain a particular data type? This is the core idea behind generics. Here is a version of the program fragment given above using generics: List < Integer > myIntList = new LinkedList < Integer > (); // 1 myIntList.add(new Integer(0)); //2 Integer x = myIntList.iterator().next(); // 3 Notice the type declaration for the variable myIntList . It specifies that this is not just an arbitrary List , but a List of Integer , written List < Integer > . We say that List is a generic interface that takes a type parameter- in this case, Integer . We also specify a type parameter when creating the list object.a type parameter when creating the list object....
View Full Document

This note was uploaded on 09/17/2011 for the course CS 2110 taught by Professor Francis during the Spring '07 term at Cornell University (Engineering School).

Page1 / 23

generics-tutorial - Generics in the Java Programming...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online