JavaEnterprise JavaTIP: Taking Advantage of Java Generics

TIP: Taking Advantage of Java Generics

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Java5 has been out for several years now, but the reality is that many corporations have just recently standardized on this version of the Java programming language. A good indication that Java5 is now widely available seems to be that many open source projects have just upgraded their minimum platform requirements to Java5 within the past year.

One of the most significant changes to the Java programming language in version 5 is the introduction of generics. Generics provide a mechanism for annotating type information for the use of the compiler. By providing this information, the compiler can in turn verify the consistent use of “genericized” classes and automatically insert explicit casts, eliminating the need for programmers to do so manually.

In Java, generics are implemented using type erasure. In other words, the generic information is available only at compile time and not translated into the compiled bytes. This allows genericized classes to interoperate cleanly with legacy, or raw, types. This approach is notably different than that of C++ templates, in which new classes are created for each new specialization.

The Java Collection Framework has been enhanced to make extensive use of generics. In fact, collections are the primary use of generics and as such they provide a good examples of how to best use generics. In the following JUnit-style example, several features of Java5, generics, auto-boxing, and the enhanced for loop are used to simplify your code.

Notice that there are no casts and no primitive-to-object conversions in this example. In the first loop, the compiler is able to unbox the object type directly from the collection; in the second, no cast is necessary to invoke the intValue() method defined by the Number interface.

Figure 1: Example JUnit Test Using Generics

Perhaps the greatest advantage of generics is the ability for library authors to express their intent more cleanly. By using generic collections, developers no longer need to choose between the advantages of type safe arrays and their more flexible collection counterparts. Generics not only document, but also help to enforce, the contents of collections and other genericized classes.

Generics can be used in more than just collection classes. In fact, developers can write their own genericized classes as well as enhance methods with generics. In the following examples, the search method accepts a typed criteria parameter and ensures that the return value is of the same type.

Figure 2: Example Generic Method Signature

Although the syntax can become verbose, Java Generics can help simplify code, express intent, and provide compile time checking of type usage. Developers should learn to take advantages of these features of Java5.

About the Author

David DeWolf is the chief technical architect and founder of Three Pillar Software, Inc. He works with mid-sized and Fortune 1000 companies to establish corporate standards that promote best practices and agile development. David has over eight years of commercial software development experience and is a member of the Apache Software Foundation’s Struts, Tiles, and Portals projects. David actively participates in the Java Community Process as a member of the Java Portlet Specification Expert Group and is the author of various online publications.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories