Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Subscription to Java Pro

email article
printer friendly
more resources

Language Features of Java Generics
Get the lowdown on J2SE's new support of generics in the core language of its upcoming 1.5 release
by Klaus Kreft and Angelika Langer

Posted March 3, 2004

Editor's Note: This is the first of two installments presenting an overview of Java Generics, a new language feature that will be supported in the upcoming release of Java 2 Platform, Standard Edition 1.5. This first installment discusses Java collections and generic treatment of types and classes. The concluding installment will look at generic treatment of methods as well as implementation and use of Java Generics.

Java 2 Platform, Standard Edition (J2SE) 1.5 will become available by mid 2004 and will include support for generic types and methods (see Resources). This new language feature, known as Java Generics (JG), is a major addition to the core language. The need for generic types stems from the implementation and use of collections, like those in the Java collection framework. Let's take a look at this new feature. Note that this discussion is based on the final draft specification of the new language feature published in July 2003 and the alpha version of J2SE 1.5 that was made available in December 2003. Currently, the beta version is available (see Resources). Neither the specification nor the compiler implementation are final releases, and you might find minor differences between the drafts and the final release. However, it is unlikely that anything mentioned here will change.

ADVERTISEMENT

Typically, the implementation of a collection of objects is independent of the type of the objects that the collection maintains. Therefore, it does not make sense to re-implement the same data structure over and over again, just because it will hold different types of elements. Instead, the goal is to have a single implementation of the collection and use it to hold elements of different types. In other words, rather than implementing a class IntList and StringList for holding integral values and strings, respectively, we want to have one generic implementation List that can be used in either case.

In Java, this kind of generic programming is achieved today (in nongeneric Java) by means of Object references: a generic list is implemented as a collection of Object references. Since Object is the superclass of all classes, the list of Object references can hold references to any type of object. All collection classes in the Java platform libraries (see Resources) use this programming technique for achieving genericity.

As a side effect of this idiom we cannot have collections of values of primitive type, like a list of integral values of type int, because the primitive types are not subclasses of Object. This effect is not a major restriction because every primitive type has a corresponding reference type. We would convert ints to Integers before we store them in a collection—a conversion that is known as boxing and that will be supported as an automatic conversion (autoboxing) in JDK 1.5 (see Resources).




Back to top













Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home