Java Generics - Wilcard Instantiations of Parameterized Types Wildcard Instantiations of Parameterized Types JavaPro Online, May 2004 Angelika Langer & Klaus Kreft Wildcard Instantiations of Parameterized Types What is the purpose of wildcards? Using Wildcard Instantiations Using Wildcards With An Upper Bound Using Unbounded Wildcards Using Wildcards With A Lower Bound Remaining Limitations Summary References With the release of J2SE 1.5 (announced for August 2004) parameterized types and methods, also known as Java Generics, will be available as a new language feature of the Java programming language. 1.1 What is the purpose of wildcards? Consider a hierarchy of shape classes with an abstract superclass Shape and several subclasses such as Circle, Triangle, Rectangle, etc. public abstract class Shape { public abstract void draw(); } public final class Circle { ... Given a collection of shapes we might want to implement a drawAllShapes() method. This method can be invoked as follows: It does, however, disallow the following invocation: A “?”
Java Generics - Introduction Language Features of Java Generics Introduction and Overview JavaPro Online, March 2004 Angelika Langer & Klaus Kreft Language Features - Overview and Introduction What is the purpose of generics? Generic Types Bounded Type Parameters Generic Methods Wildcard Instantiations of Parameterized Types Summary of Java Generics Language Features Implementation of the Java Generics Language Features Translation of Generics Type Erasure Summary References Language Features J2SE 1.5 will become available by mid of 2004 and will include support for generic types and methods (see / JDK15 /). What is the purpose of generics? The need for generic types stems from the implementation and use of collections, like the ones in the Java collection framework (see / JDK15 /). In Java, this kind of generic programming is achieved today (in non-generic Java) by means of Object references: a generic list is implemented as a collection of Object references. Generic Types Bounds Here is an example:
Generic Types, Part 1 O'Reilly Book Excerpts: Java in a Nutshell, 5th Edition by David Flanagan Java in a Nutshell, 5th Edition Generic types and methods are the defining new feature of Java 5.0. A is defined using one or more and has one or more methods that use a type variable as a placeholder for an argument or return type. For example, the type java.util.List<E> is a generic type: a list that holds elements of some type represented by the placeholder E . In order to use a generic type like this, you specify actual types for the type variable (or variables), producing a such as List<String> . [1] The reason to specify this extra type information is that the compiler can provide much stronger compile-time type checking for you, increasing the type safety of your programs. The collections classes of the java.util package have been made generic in Java 5.0, and you will probably use them frequently in your programs. Typesafe Collections Generic types solve the type safety problem illustrated by this code.
Generic Types, Part 2 O'Reilly Book Excerpts:Java in a Nutshell, 5th Edition by David Flanagan Editor's note: In part one of this two-part excerpt from Java in a Nutshell, 5th Edition, David Flanagan described how to use generic types. This week David details how to write your own generic types and generic methods, and concludes with a tour of important generic types in the core Java API. Writing Generic Types and Methods Creating a simple generic type is straightforward. First, declare your type variables by enclosing a comma-separated list of their names within angle brackets after the name of the class or interface. We begin this section with a simple generic type, which we will subsequently refine. import java.util.*; /** * A tree is a data structure that holds values of type V. * Each tree has a single value of type V and can have any number of * branches, each of which is itself a Tree. */ public class Tree<V> { // The value of the tree is of type V. Type variable bounds Wildcards in generic types Tree<?
Generics and Method Objects Published on ONJava.com ( See this if you're having trouble printing code examples by William Grosso, author of Java RMI 11/14/2001 Abstract In the first article in this series, I introduced a distributed translation service and showed how using a simple command object framework to encapsulate retry logic simultaneously simplified the client application and made it more robust. The second article in this series built on this framework to implement a caching system for stubs to remote servers. In this article, I'll introduce you to the new Generics Specification (which came out of the Java Community Process) and then rebuild the command object framework using it. Most of this article is concerned with generic classes and generic methods (hereafter simply generics). The source code for this article is available as a zip file. Frameworks, Libraries, and Type Safety By any count, the number of libraries available for the Java programmer is enormous.
Reified Generics for Java Many people are unsatisfied with the restrictions caused by the way generics are implemented in Java. Specifically, they are unhappy that generic type parameters are not reified: they are not available at runtime. Generics are implemented using erasure, in which generic type parameters are simply removed at runtime. That doesn't render generics useless, because you get typechecking at compile-time based on the generic type parameters, and also because the compiler inserts casts in the code (so that you don't have to) based on the type parameters. Generics are implemented using erasure as a response to the design requirement that they support migration compatibility: it should be possible to add generic type parameters to existing classes without breaking source or binary compatibility with existing clients. I wrote about this two years ago. While solving one set of problems, erasure adds a set of its own problems. It isn't too late to add reified generics to Java.
Java theory and practice: Generics gotchas Generic types (or generics) bear a superficial resemblance to templates in C++, both in their syntax and in their expected use cases (such as container classes). But the similarity is only skin-deep -- generics in the Java language are implemented almost entirely in the compiler, which performs type checking and type inference, and then generates ordinary, non-generic bytecodes. This implementation technique, called erasure (where the compiler uses the generic type information to ensure type safety, but then erases it before generating the bytecode), has some surprising, and sometimes confusing, consequences. While generics are a big step forward for type safety in Java classes, learning to use generics will almost certainly provide some opportunity for head-scratching (and sometimes cursing) along the way. Note: This article assumes familiarity with the basics of generics in JDK 5.0. Generics are not covariant Because ln is a List<Number>, adding a Float to it seems perfectly legal.
Generics (Java) Tutorial Oracle Technology Network > Java Software Downloads View All Downloads Top Downloads New Downloads What's New Java in the Cloud: Rapidly develop and deploy Java business applications in the cloud. Essential Links Developer Spotlight Java EE—the Most Lightweight Enterprise Framework? Blogs Technologies Contact Us About Oracle Cloud Events Top Actions News Key Topics Oracle Integrated Cloud Applications & Platform Services
Diagnosing Java code: Java generics without the pain, Part 1 J2SE 1.5 -- code-named Tiger -- is scheduled for release near the end of 2003. I'm always in favor of gathering as much advance information on upcoming technology as possible, so this article is the first in a series on the new and reformatted features available in version 1.5. Specifically, I'd like to talk about generic types and highlight the changes and tweaks in Tiger designed to support them. In many ways, Tiger promises to be the biggest leap forward in Java programming so far, including significant extensions to the source language syntax. Let's start off with an introduction to what generic types are and what features are being added to support them. Casts and errors To understand why generic types are useful, we turn our attention to one of the most significant causes of bugs in the Java language -- the need to continually downcast expressions to datatypes more specific than their static types. class Hashtable { Object put(Object key, Object value) {...} Back to top Listing 4.
Using and Programming Generics in J2SE 5.0 A Java collection is a flexible data structure that can hold heterogeneous objects where the elements may have any reference type. It is your responsibility, however, to keep track of what types of objects your collections contain. As an example, consider adding an int to a collection; since you cannot have collections of primitive data types you must convert the int to the corresponding reference type (i.e. Integer) before storing it in the collection. Now, when the element is extracted from the collection an Object is returned that must be cast to an Integer in order to ensure type safety. If the compiler could keep track of the element type, you do not need to keep track of what collections you have and the need for casting would be eliminated. Generics are one of the most frequently requested language extensions to Java, and they have been finally added in J2SE 5.0. The Need for Generics As you can see, when an element is extracted from the list it must be cast. Ex1.java Ex2.java "?