In Anticipation of Tiger

Theme 5: Ease of Development

The Java language and syntax are being enhanced to make code more readable, expressive, comact, safer, and easier to develop, without sacrificing compatibility. These new language features are being incorporated after several years of discussion and comparison of Java to other programming languages. These features are the most anticipated amongst the Tiger release contents and have spawned numerous threads of discussion within the developer community. The changes will come in the form of class file format changes, generic (a.k.a. parameterized types like C++ templates) support, a 'foreach' type of looping construct, automatic conversion between primitive, and corresponding object data types, type-safe enums, an improved syntax for using static constants, and the use of metadata tags which allow the developer to insert simple declarative tags in the source code for which the compiler generates the necessary boilerplate code.

These changes will have an impact on the way developers write Java programs. Taking advantage of these changes will require the developer to learn new language features, new syntax, and must become part of the developer's arsenal. Let's look deeper into each of these new features.

Class File Format Changes

These changes will modify and extend the Java Class File format to support updates to the Java platform and language specifications. The changes will add support for quicker and more efficient byte code verification, class literal support directly from the class file format, and will increase certain implicit size limits within the class file. These changes will be transparent to the application developer. No code changes would be required for existing codebases.

Support for Generics

Generic support is the ability to specify a particular type of object used in a collection, rather than default to using the Object type for generic behaviour. These notions of genericity are based on parametric polymorphism. These concepts are best explained with an example illustrating the benefits and syntax changes.

Let's say you have a method that prints all the Strings in the collection passed to it in lower case.

The things to notice here are that an explicit cast to String is required when iterating through the Collection. This is error prone. If a programmer inadvertently passes a collection of any other type, this method would compile fine, only to blow up at runtime with a ClassCastException. The precondition for this code to correctly execute is that the Collection must contain Strings. This fact is not explicit from the method signature, but rather is expressed in the documentation for the method.

Here, we explicity specify what type of Objects the Collection will contain, thereby eliminating the need for casting. The programmer's intent is now absolutely clear and is part of the method signature rather than the documentation. If a programmer inadvertently passes a collection of any other type, this method would not even compile. This eliminates runtime ClassCastExceptions altogether and provides compile-time type safety.

The 'foreach' type of construct: Tiger plans to introduce an additional for loop syntax with similar capability as the 'foreach' looping construct found in so many other languages. This addition is mainly to increase developer productivity and reduce the amount of code required to iterate through loops.

Let's say you have to loop through a Collection of Strings. Currently, you may achieve the loop by using a while statement or a for statement, as illustrated below:

Both these constructs require the creation of an Iterator object and explicit specification on how to iterate through the collection, by using the hasNext() method.

The 'enhanced for' construct makes the code look like:

Collection c;
for( Object o : c ) {
//Process element
}

This reads as 'for each Object o in Collection c'. Combined with generics support, this could be:

Collection c;
for( String o : c) {
//Process element
}

The 'foreach' type construct eliminates the need for Iterator creation and delegates all details of how to iterate through the collection to the compiler. It also reduces the amount of code required to iterate through collections.

Automatic conversion between primitive types and corresponding object types: Java has a 'split type' system, which means there is a difference in the way primitive types and object types are handled. This forces the programmer to perform manual conversions between primitive types and corresponding object types in code.

If you would like to add a primitive int to an Integer object, you would have to do:

In Tiger, the conversion between primitive types and their corresponding Object type will be automatic. This feature is also referred to as 'autoboxing of primitives'.

The code is now as easy as:

int i;
Integer j;
int k = i + j;
Integer kObj = i + j;

This feature almost removes all distinction between primitive types and corresponding object types for the programmer. It further allows the addition of primitive types to Collections, so this

Collection c;
int i;
c.add(new Integer(i));

becomes

c.add(i);

Typesafe enums: An enum is a bounded set of distinct values that a particular object can take. They are similar to a grouping of constants representing all permissible values for a particular object. Tiger intends to add language-level support for the typesafe enum pattern. This pattern is best described by an example:

Because the typesafe enum type is just like a class, we could add methods and fields and even use them in collections.

Improved syntax for using static constants: To use constants in Java, the programmer must either fully qualify the constant reference or implement an interface defining the constants. Consider an example of what is described as the 'Constants Antipattern':

The import statement will make all static members of the Constants class available to the Year class. This compact syntax provides the benefits of not having to fully qualify the constant reference and overcomes the disadvantages of implementing an interface.

Metadata facility to annotate source code: Programmers writing EJBs and RMI-based apps need to define a lot of boilerplate code to implement their objects. For example, to implement a Remote object (in the RMI sense), a programmer must define the interface.

This also annotates the source code by marking it to have certain characteristics.

There is still much discussion about this feature, its implementation, extent, and scope. More details on this will be available as we come closer to the release date.

Summary

As you can see, this release is going to be a big one, especially with respect to language changes. Overall, with the introduction of these changes and additional APIs will come with some amount of learning curve but will make Java more developer friendly and easier to work with. The language changes widen Java's reach to cross-language developers. The performance enhancements will make this Java the fastest one ever. The focus on XML and Web Services makes Java a language of choice for modern network programming paradigms. This release will strengthen the mind-share and further increase the popularity of the platform.

About the Author

Apu Shah, a Java developer and avid follower of breaking Java trends since 1995, also writes a biweekly column on the events of the JCP on developer.com. If you have any thoughts on these features or would you like to see other features in Tiger, feel free to contact him at apu@jcpwatch.org.