The release of Java 2 Platform Standard Edition 5 (J2SE 5) has changed Java
programming forever. These are strong words, but they are not overstatement.
Unlike the previous Java upgrades, which offered important, but incremental
improvements, J2SE 5 fundamentally expands the scope, power, and range
of the language. Not since its original version nearly a decade ago has a
release of Java been so important, or so eagerly awaited. During development,
the codename for J2SE 5 was "Tiger". It was a fitting name.
J2SE 5 contains major enhancements to both the Java language and its core
libraries. Many of these, such as generics, autoboxing, and enumerations, have
been desired by Java programmers for many years. Others, such as metadata, are
forward-thinking innovations that point toward even more far-reaching
developments in the future. In both cases, they will fundamentally affect the
way that all Java programmers write code.
To understand why J2SE 5 is so important, consider the following list of
major new features.
Generics Generics have been long awaited by Java programmers. At its
core, the term generics means parameterized types.
Parameterized types are important because they enable the programmer to create
generic methods, classes, and interfaces in which the type of data operated upon
is specified as a parameter. As a point of reference, generics in Java are somewhat similar to templates
in C++.
Generics enhance Java programming in two important ways.
- Code Re-use
Generics expand the programmer's ability to re-use code. With generics, it is possible to
create a single class, for example, that automatically works with different
types of data. You no longer need to create a separate version of the class
for each different data type.
- Type Safety
Generics add type safety.
Because the generics mechanism implicitly handles type conversions, explicit
casts are not required. Thus, casting errors are avoided and the compiler can
flag type-mismatch errors.
Therefore, with generics a programmer can create classes, interfaces, and
methods that will automatically work in a type-safe manner with different
types of data.
Generics are the single most important feature added by J2SE 5 because it
has far-reaching impact that is felt throughout the entire Java language. To
understand why, consider the following. The addition of generics required an entirely new
syntax element. It resulted in
changes to many of the classes, interfaces, and methods in the core API. The Collections
Framework was particularly affected. Moreover, the expressive power generics add to the
language profoundly alters the way that Java code is written.
Generics have changed Java. Soon, all pre-generic code will be obsolete.
Generics simply cannot be ignored.
Metadata The new metadata facility lets you embed annotations into a
program. The annotations are then processed by various programming tools. For
example, a tool might generate Java source code as requested by an annotation. The Java metadata facility is part of a growing trend in
programming in which the programmer specifies an action but leaves it to a
tool to actually provide the code. Such an approach reduces the amount of
repetitious code that a programmer must enter by hand.
Autoboxing/unboxing Autoboxing is the feature that lets Java
automatically encapsulate a primitive type into an object of its corresponding
type wrapper. For example, an int value is automatically boxed
into an Integer object. Auto-unboxing is the reverse process; a primitive type represented by a
type-wrapper object is automatically converted into its corresponding primitive type. Because they
streamline many common programming operations, autoboxing/unboxing are an
addition that all Java programmers will welcome.
Enumerations In essence, an enumeration is a named list of constants.
The enumeration type is supported by the new keyword enum. In the past,
when such constants were needed, they were typically coded as static final.
An enumeration offers a much better alternative. In Java, enum declares
a class type, which means that an enum can have methods and fields.
This gives enum capabilities in Java that surpass its counterpart in
other languages.
Enhanced for Loop J2SE 5 adds a "foreach" capability to the for
loop. This enhancement lets the for automatically cycle through the contents of a
collection or an array, from beginning to end. In programming, such
tasks are quite common. For example, to sum the values in an array, each
element in the array must be examined. Previously, one would write such a loop
like this:
for(int i=0; i < nums.length; i++)
sum += nums[i];
Using the enhanced for
loop, the preceding statement can be recoded as shown here.
for(int v : nums) sum += v;
The new form automatically obtains each value in the array, one at a time,
beginning with the first element, and stores that value in v. This new
"foreach" feature not only simplifies a common construct, but makes
it safer, too, by preventing boundary errors.
Varargs Varargs, which is short for variable-length arguments,
is a new feature that lets a method take a variable number of arguments. The
addition of varargs makes it easier to code methods for which the length of
the argument list may vary from call to call.
Formatted I/O J2SE 5 offers an alternative way to input or output
formatted data. For output, the programmer can use the new printf( )
method, which is based on the well-known C language printf( ) function.
Formatting capabilities are also available through the new Formatter
class. The new Scanner class assists with reading formatted input.
Static Import The static import feature streamlines access to the
static members within a class or interface. When using static import, it is possible to
refer to static members directly by their names, without having to qualify
them with their class or interface names. For example, prior to the static import facility,
a method in Java's math library needed to be qualified with the Math
class, as in Math.cos( ). With static import, the program can import
the static members of Math and then refer to the static methods
directly, as in cos( ).
Changes to the Collections Framework J2SE 5.0 completely updates
the Collections Framework for generics. This causes changes to the way that
many of its classes, interfaces, and methods are declared. J2SE 5.0 also updates the Collections classes for use by the enhanced for
loop. Accompanying these major changes, are many smaller, but valuable
enhancements that further expand the power and range of the
Collections Framework.
The Concurrent API The concurrency API supplies many features that have
long been wanted by programmers who develop concurrent (i.e.,
thread-intensive) applications. For example, it offers semaphores, cyclic barriers, countdown latches, thread pools, execution managers, locks, several
concurrent collections, and a streamlined way to use threads to obtain
computational results.
As the preceding list shows, the additions to Java are quite significant.
Instead of simply tweaking a feature here or adjusting a nuance there, J2SE 5
fundamentally expands the language. This level of innovation is seldom seen in a
language as mature and widely used as Java. In general, computer languages
usually follow a "life cycle" that begins with a burst of creativity
and innovation, followed by a long period of widespread use and stability, and
ending with the language being used to maintain "legacy" code. Java is
now in the middle period of its life cycle. It would normally be settling down
into a long period of tranquility, resting on its laurels, so to speak. But Java
isn't following the rules!
Since the beginning, Java has been at the center of a culture of innovation.
Its original release redefined programming for the Internet world. The Java
Virtual Machine (JVM) and bytecode changed the way we think about security and
portability. The applet (and then the servlet) made the Web come alive. The Java
Community Process (JCP) redefined the way that new ideas are assimilated into
the language. It should come as no surprise that Java continues to push the
frontiers of programming.
With the release of J2SE 5, the world of Java programming has changed. Many of
the techniques that programmers have relied on in the past are now outdated,
having been replaced by better, more powerful constructs. Programmers who fail
to adopt the new strategies will soon find themselves left behind. Frankly, in
the competitive world of programming, no Java programmer can afford to be left
behind.