J2SE 1.5 - Effective Java Programming with Tiger

Typesafe Enumerations

What is enum?

Enum is a special kind of class declaration. A typesafe enum facility defines a fixed set of constant legal values for types. This feature is borrowed from the C language and has been incorporated into the Java language in the Tiger release.

Advantages of enum

The proposed enum facility has more advantages when compared with the traditional int enum approach in Java. The advantages are outlined below.

enum provides compile time type safety

enum provides proper name spacing for its types

Performance comparable to int constants

Typesafe constants do not require a compilation when clients modify constants

Printed values are informative

enum can be used in collections because they are objects

enum is a special kind of class declaration; hence, one can add arbitrary fields and methods

enum can be made to implement arbitrary interfaces

Traditional enum pattern

Listing 5 shows the commonly used pattern for enumerated types in Java that suffer from many drawbacks. In this approach, whenever you modify the client code, you need to recompile.

Typesafe enum pattern

Java provides an alternative method to overcome the drawbacks of the traditional approach. This is called the typesafe enum pattern. This pattern defines a class that represents a single element of an enumerated type that doesn't provide any public constructors to it. For each constant in the enumerated type, provide static final fields. Listing 6 shows this pattern approach. This pattern provides compile time type safety by allowing that only any of the six continents defined can be passed to a method which defines a parameter of type Continent.

Autoboxing

What is autoboxing?

Developers are burdened when they convert primitive types to wrapper (reference) types (for example, converting int to Integer type). Adding primitive types to a collection is not possible unless they are converted to the corresponding reference type. To overcome this drawback of the current type system, the need for automatic conversion of primitive type data to their corresponding wrapper type data was proposed. This conversion mechanism is known as autoboxing.

Listing 8 illustrates the code that achieves autoboxing in a collection. In this code sample, the primitive type int is added to a collection that holds an Integer reference. The compiler takes care of the type conversion for you.

Enhanced for Statement

What is wrong with the current for statement?

The current for statement is very efficient and powerful in all aspects. But, it is not optimized when it iterates over a collection because the iterators are used only for getting elements out of a collection and serve no other purpose. Generics helped make this situation better by adding the type safety to the collection. This resulted in proposing "enhanced for" statements with generics additions to it. With this new feature, the compiler takes care of the iterator for you. Listing 9 details the usage of "enhanced for" statements.

Static Import

Rationale for static import

In the Java world, when using any mathematical functions from the "java.lang.Math" package or when using named constants, one has to prefix the method name or field name with the class name. This way of coding looks more verbose. To provide convenience to the developers, the concept of a static import was proposed. By using this, one can allow the import of static methods and fields similar to classes and interfaces.

When accessing named constants, you need to prefix with the class name. For example,

Math.PI

This approach makes code more verbose. The static import helps overcome this approach. This is a simple feature, but yet a convincing one for developers.

Conclusion

The features that were discussed in this article are only a subset of features that are planned to ship with Tiger. These features are developer savvy and yet powerful additions to the Java Programming language. Most of the listings in this article are tested with the Generics early access compiler version 2.2. Although enough features are targeted towards the Tiger release, the final specification will become soon available for review once the component JSRs releases them.

About the Author

Arulazi Dhesiaseelan has been working as a Senior Software Engineer for the Hewlett-Packard Company, India. He has a Master of Computer Applications degree from PSG College of Technology, India. He has more than three years of industry experience. He was also involved in the UDDI4J project hosted at http://uddi4j.org. He has been working on Web Service related technologies such as WSDL, UDDI, and SOAP. Currently, he is involved in developing an object-based infrastructure for mobile solutions. He can be reached at aruld@acm.org.