Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

JDK 1.5 Preview: Addition of Generics Improves Type Checking

Generics is one of the hottest features of the upcoming JDK 1.5 release. This article describes the new generics feature by investigating the example pre-release implementation provided by Sun. You'll use the generics feature to create an extensible multimedia application, and show how generics make the code simpler.


advertisement
enerics is one of the hottest features of Sun Microsystems's upcoming JDK 1.5 (Tiger) release. Programmers and researchers have been clamoring for a feature like this for years, and an impatient few have even implemented their own prototypes already. Generics enables the creation of parameterized classes and methods, allowing Java developers to create custom variations of their code for different types.

Unlike JDK 1.4, Tiger isn't just a set of new standard libraries—it features actual changes to the language itself. Generics is the most profound of these changes. Long a part of advanced academic languages, this new feature is a welcome addition to Java.

This article introduces the new generics feature by examining Sun's example pre-release implementation. It demonstrates how to use generics in a sample program, an extensible networked multimedia framework (NMF). Finally, it goes over using generics in the Collections classes.



What Is Generics?
Generics is very similar to C++ templates, both in syntax and semantics. However, like most things in Java that are similar to C++, it is simpler. Since generics has a clear syntax, the following quick example will go a long way in explaining it:

public class Mailbox<Thing> { private Thing thing; // ... }

This is a fragment from the source code for NMF. The first thing you should notice is the "<Thing>" following the class name. This is a type parameter, which says you can create variations of the Mailbox class by providing different types to take the place of Thing. This means that Mailbox is a parametric class, a class that takes one or more parameters.

Thing isn't really a type or class—you haven't created a file called Thing.java that contains a class definition for Thing. Rather, you'll have to supply something to take the place of Thing. Here's how you instantiate the parametric class:

Mailbox<String> myMailbox = new Mailbox<String>();

Now Mailbox<String> clearly is the full name for a Java type. It means "the Mailbox class, with Thing replaced by String." You could also use Mailbox<URL>, which would be "the Mailbox class, with Thing replaced by URL."

In NMF, a Mailbox is an object that lets you send and receive messages of different types. A Mailbox<URL>, for example, lets you send and receive URLs, while a Mailbox<String> lets you send and receive Strings.

The nice thing about this is that you don't have to create two different classes to send and receive two different kinds of messages. Instead, you can create a single class, parameterized by the type variable Thing. You simply program this class to send and receive Things. Instantiating the Mailbox class with various types replaces the Thing type variable with actual types such as String and URL.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap