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 : Page 4

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
Generics Code Meets Non-generics Code
Internally, Mailbox doesn't do all that much. When an object is sent, the Mailbox puts the object on the Client's send queue, and when an object comes in to the Client, the Client puts it on the Mailbox's receive queue.

The interaction between the two is interesting, however. Mailbox is a parameterized type, which means that each separate instance deals with a different message type. Client, on the other hand, deals only with Object types. In this sense, Client is written in the old, pre-generics style. Code written using generics inevitably will have to interact with code not written with generics. Such instances will require a cast.

In the case of NMF, the cast happens when objects are coming from the server. The Client determines to which Mailbox the object should go. It treats the Mailbox as an Enqueuer, which is an interface defined like this:



public interface Enqueuer { public void enqueue( Object object ); }

The Client passes the Object that it has received to the Mailbox via this method. Here's Mailbox's implementation of enqueue():

public void enqueue( Object object ) { Thing thing = (Thing)object; readQueue.put( thing ); }

Here's where you've hidden the inevitable cast. Client deals with Objects, while Mailbox deals with Things, and here is where the two meet.

In actuality, an interface isn't required here—it's just a nice way of structuring things. But the important thing is to recognize that generics-based code and non-generics-based code have something of an impedance—the generics code does away with run-time type checks, while the old-style code still needs them. When objects are passed from old-style code to new-style code, it generally means a typecast will be produced—and thus the possibility of a type exception.

Generics and Collections Classes
The NMF program actually uses generics in several other places, but not by defining new parameterized classes. Rather, it uses existing parameterized classes from the Java Collections classes.

The Collections classes in the java.util package are used to create various kinds of object collections (lists, sets, maps, and so on). They also provide useful iteration classes. However, they can be a bit annoying. During your Java programming, you've probably typed something this more times than you'd like to remember:

for (Enumeration e = vector.elements(); e.hasMoreElements();) { String s = (String)e.nextElement(); } Or, using the newer classes, something like this: for (Iterator it = list.iterator(); it.hasNext();) { String s = (String)it.next(); }

It's a lot to type over and over. Generics can get rid of that annoying cast. All of the Collections classes have been turned into parameterized classes, generally in the obvious way. For example, a List of Strings can be created like this:

List<String> myStrings = new ArrayList<String>();

And you can use it like this:

for (Iterator<String> it = list.iterator(); it.hasNext();) { String s = it.next(); }

See? The cast is gone. Of course, you had to add that <String> in there, so you haven't actually saved any typing in this case. But you would if you used the variable it more than once inside the loop.

The real win will come when the full JDK 1.5 feature set is implemented. It will contain a new for syntax that will make iteration very concise:

for (String s : list) { // ... }

It seems that Java's designers have finally realized the centrality of iteration, and have made it as concise as possible. As noted language hacker Paul Graham has said, "succinctness is power." And that for loop is about as concise as you can get.

Using Collections Classes
This section looks at a few places where the NMF program uses the Collections classes.

As you'll recall, the Client receives messages from the server and hands them to the appropriate Mailbox, based on the channel on which the message was sent. Client carries out this mapping using a java.util.Map object. More precisely, a Map that maps channels (Strings) onto Mailboxes, where the Mailbox is represented as an Enqueuer:

private Map<String,Enqueuer> mailboxes = new HashMap<String,Enqueuer>();

Using the Map is now cast-free:

Enqueuer mailbox = mailboxes.get( channel );

Parameterized collections are used in a few other places in the code:

  • The WhiteBoard class uses a Set<Line> to store the set of lines that has been drawn.
  • The Server class uses a List<Responder> to store objects that respond to Client requests.
  • The Queue class (not part of collections) is a simple blocking queue, parameterized by the type of the object it contains. This class is used as the read queue in Mailbox and the write queue in Client.

Finally, Generics in the JDK
Generics is a well-understood language formation that has not caught on particularly well in commercial languages, but it has a strong theoretical underpinning and a solid prototype implementation that is available now.

Generics does more than simply eliminate extra casts. It provides extra typing information to the compiler, allowing it to perform certain typing checks at compile-time that previously had to be done at run-time. In combination with the Collections classes and the new for loop syntax, generics can be very powerful. Watch for it in the upcoming release of JDK 1.5—finally.



Greg Travis is a freelance Java programmer and technology writer living in New York City. After three years in the world of high-end PC games, he joined EarthWeb, where he developed new technologies with the then-new Java programming language. Since 1997, he has been a consultant in a variety of Web technologies. Reach him by e-mail .
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap