Time to open the Champagne -- Java 1.5 is out, and the language has finally come of age! With the new Java 1.5 specification, Java now contains features that make it feel like a proper "grown-up" language. The rest of this article will introduce you to these new features. To try out the features for yourself, simply download Java 1.5 from Sun’s website and give it a whirl. Note that you’ll need to compile the code using the –source 1.5 option; otherwise, you’ll get compilation errors when using the new features.
The idea of the new Meta-Data facility is to add annotations to your code that do not alter its semantics, but provide additional information that can be used by a compiler or other utilities. An example of such an annotation that you probably use already is the @deprecated tag. Marking a method as deprecated does not change the operation of the method, but does generate a warning when the compiler encounters it.
Java 1.5 introduces a new tag, called @override, with which the programmer can explicitly state the intention to override a method of the superclass. If the superclass contains no such method, then the compiler generates an error:
public
@Overrides String toSring() { return value+" of "+suit; }
In this example, I missed out the t of String in the method name, so that the method in fact does not in fact override the toString() method of java.lang.Object. When the code is compiled it produces the following error:
Card
.java:22: method does not override a method from its superclass public @Overrides String toSring() { ^ 1 error
Defining your own tags to contain meta-data is somewhat more involved, and is therefore an activity that most Java programmers will eschew. On the other hand, I can imagine that once tags are defined, they would be used by large numbers of programmers. Depending on how the meta-data facility is configured, annotations can be available in the source-code, at compile-time, or even in the class files, accessible through Java reflection. This promises rich and powerful sets of annotations that can be used at different stages of the development lifecycle for many different purposes.
Conclusions
There are some other new features, too. The features that I found most interesting are the following:
Extensions to JDBC RowSets to provide caching and filtering.
Two new look-and-feels: Synth, a skinnable look and feel, and Ocean, a new theme for Metal.
There is now printing support for the Swing JTable.
Improved diagnostic abilities: using Thread.getStackTrace(), it is now possible to programmatically generate a stack trace as an array of StackTraceElements.
Java1.5 is an impressive step forward, although not all of the changes are popular among developers. I have read several discussions in which some people express the sentiment "Why do I need that -- I can get on perfectly well without it!". That may be true. But most of the language changes in Java 1.5 aim to reduce programming errors and increase productivity. Developers who do not embrace such changes risk being left behind by more receptive, and ultimately more productive, peers.
Although I welcome the changes from a technical standpoint, one cannot help but wonder what would have been included in Java 1.5 were it not for the driving influence of .NET and C# in the marketplace. I believe that new features such as enumerations and generics are a direct response to the features of C#. The intense competition for market share between Sun and Microsoft is not only an interesting sideline sport, it is also accelerating the technological advances from which we, as developers, will benefit.