In the main track we gave an overview
of Java 2 Platform, Standard Edition 5.0 (J2SE 5.0) . We indicated
that most of the changes fall into the ease of development
(EoD) category. With a few important exceptions, the changes do
not add new functionality but rather provide an easier way of doing
the same things you could do before but with less code and better
compiler-time error detection.
We list below the most important of the many EoD improvements in
Java 5.0, roughly in the order in which they are encountered in
the rest of this book, not in the order of importance.
Note: Most of these enhancements
to the language can only be appreciated after having had experience
with programming in Java. If you are completely new to object oriented
programming and Java, you can come back to this section after you
complete Part I.
Autoboxing and unboxing
Chapter 2 explains
that Java has primitive types like int
for integers, and Chapter
3 explains "object" types like Integer
The difference between the two types is very important as we
will see. Previous versions of Java made it necessary to explicitly
convert between the primitive types and the object types.
In Chapter 3
we examine the so-called autoboxing and unboxing feature added
with J2SE 5.0 that removes the need for explicit conversions
in most cases and thus improves code readability and removes
boilerplate code and sources of errors.
Enhanced for loop
Chapter 2 looks
at the several types of looping structures in the Java language,
one of which is the for loop (quite similar to the C/C++
for loop). Version 5.0 includes an enhanced for loop
syntax that reduces code complexity and improves readability.
We introduce the enhanced for loop in Chapter
2: Supplements and in Chapter
10: Generics after we have described the object types with
which the enhanced for loop works.
Metadata
In Chapter 4 we encounter
the @Override
annotation. It falls under the category of metadata or "data
about data." In this case, it is better thought of as "data
about code." The new metadata facility, also called the annotation
facility, is designed to use an "annotation" in your code that
greatly reduces much of the boilerplate code that would be required
in previous versions of Java.
An annotation is a new 5.0 language element that begins with
"@".
Some annotations are processed by the javac compiler and some
require the new annotation processing tool apt. There are currently
only three annotations in the beta release of version 5.0. However,
now that the metadata framework is available, we anticipate
the appearance of many useful annotations and annotation processors
in the future.
Formatted input and output and varargs
In Chapter
5: Tech we discuss how to format numerical output with Java.
Version 5.0 finally adds the oft-requested ability to produce
formatted output easily in the form of a printf() method that
behaves very similarly to the printf()
function in the C/C++ stdio library. There is also a new formatted
input feature (the Scanner
class) that is described in Chapter
9: Java.
Both these features rely on another new feature known as "varargs,"
which stands for "variable argument list" in which the number
of parameters passed to a Java method is not known when the
source is constructed. (This is also referred to as variable
arity methods). Varargs is a very cool new feature that
can be of value in your own code, not just in the new printf()
feature.
Chapter 10:
Java presents another EoD enhancement that provides for
automatic format and output of the elements of an array (see
the java.util.Arrays
class). This feature really has nothing to do with printf
or varargs,
but we mention it here because it eases the amount of work that
was necessary in pre-5.0 releases to output all the elements
in an array in a nicely formatted style.
Static import
Release 5.0 includes a new technique for accessing Java static
methods and constants in another class without the need to include
the full package and class name every time they are used. (We
explain what the terms class,
package,
static,
import, etc. mean in Chapters 3-5.) This new "static
import" facility makes your code easier to write and, since
there's less of it, less error-prone. We discuss static import
in more detail in Chapter
5 after discussing import in general.
New pack200 hyper-compression JAR format
Chapter 5: Java
discusses JAR (Java Archive) files used to combine and compress
Java class files. We also look at the new pack200 format that
compresses JAR files very tightly, reducing bandwidth and saving
download time. (This is not really an EoD change, but more of
an "ease of deployment" change.)
Graphics system improvements
Release 5.0 includes numerous bug fixes and minor tweaks to
Java's graphics subsystems known as AWT and Swing (see Chapter
6 and Chapter
7), including reduced memory usage. In the EoD area, perhaps
the biggest improvement is that it is no longer necessary to
call getContentPane()
when using Swing components. See Chapter
6 for details. Other enhancements include an improved popup
menu support, improved printing support for some graphics components,
and the ability to query for the mouse location on the desktop.
New concurrency features
Chapter 8 discusses
Java's multithreading support that has been present since version
1.0. Release 5.0 adds new capabilities that greatly enhance
the multithreading features of Java. Some of these additions
depend upon the generics concept (see next item), so we wait
until Chapter
10 to introduce these important new capabilities.
Generics
In Chapter 10
we introduce the new generics feature, a large and important
subject that we do not have space to cover in detail in this
book. Java is already a very type-safe language, which simply
means that every variable has a specific type and that only
compatible types can be assigned to each other. However, the
use of generics brings an even greater amount of type safety
to the Java language.
Java includes a number of "object containers", such
as the ArrayList
class, that can contain "objects" of many different types. When
retrieving an object from one of these containers, it must be
retrieved as a very basic type and then converted back to the
original type. If, however, an incorrect type is added to the
container, then an error occurs at runtime during the conversion
attempt. The use of generics makes it possible for the object
containers to require that only certain types can be placed
into them, else a compile time error occurs. Since mistakes
are found at compile time, runtime safety and correctness is
improved. In addition, since the specialized containers only
contain items of the desired type, retrieval of items from the
containers is easier since no explicit conversion to the desired
type is necessary
You are not required to use the generics approach but the 5.0
compiler will give a warning for code that uses the older containers.
Enumerated types
Chapter
10 presents a feature of C/C++ that many programmers have
missed in Java. Version 5.0 adds an enumerated type using the
enum keyword. The new Java enumerated type includes all the
features of C/C++ enum and more, including type safety.
New StringBuilder
class
We discuss this new class in Chapter
10, along with the older StringBuffer
class. Both are used in the building, concatenating, and appending
of string types, but the new class has improved performance.
Changes to ease RMI development
Chapter 18
explains the Remote Method Invocation (RMI) techniques, including
a simple but important change in J2SE 5.0 that makes RMI development
simpler.
We do not go into great depth on these changes here or in the book.
See the book by McLaughlin for details. There is also much documentation
available online at java.sun.com.
See the references below.
References and Web Resources
- Calvin
Austin, J2SE 5.0 in a Nutshell, May 2004, Sun Developer
Network Site.
- Calvin
Austin, Take the Fast Track to J2SE 1.5: JCP experts listened
and developed the best developer platform. Discover how new J2SE
1.5 features can streamline your code, JavaPro, June 7, 2004.
- Java
Programming Language - Enhancements for JDK 5
- Brett McLaughlin and David Flanagan, Java 1.5 Tiger, A Developer's
Notebook, O'Reilly, 2004
- Class
Data Sharing - technique for speeding up program startup times.
- Java
Web Start at java.sun.com
Most recent update: Oct.19, 2005
|