Language Features Added in Java 5
Several useful syntactic elements were introduced in Java 5. All these features are supported by an
updated compiler, and all translate to already defined Java bytecode, meaning that virtual machines can
execute these features with no need for an update:
❑ Generics: A way to make classes type-safe that are written to work on any arbitrary object type,
such as narrowing an instance of a collection to hold a specific object type and eliminating the
need to cast objects when taking an object out of the collection.
❑ Enhanced
for loop: A cleaner and less error-prone version of the for loop for use with iterators.
❑ Variable arguments: Support for passing an arbitrary number of parameters to a method.
❑ Boxing/unboxing: Direct language support for automatic conversion between primitive types
and their reference types (such as int and Integer).
❑ Type-safe enumerations: Clean syntax for defining and using enumerations, supported at the
language level.
❑ Static import: Ability to access static members from a class without need to qualify them with a
class name.
❑ Metadata: Coupled with new tools developed by third-party companies, saves developers the
effort of writing boilerplate code by automatically generating the code.
These features update the Java language to include many constructs developers are used to in other lan-
guages. They make writing Java code easier, cleaner, and faster. Even if you choose not to take advan-
tage of these features, familiarity with them is vital to read and maintain code written by other
developers.
Generics
Java 5 introduced generics, also known as parameterized types. Generics allow you to write a class that
can operate on any type but that specific type is not specified until declaration of an instance of the class.
Because this type is not specified as part of the class definition, the class becomes generic, gaining the
ability to work on any type specified. The most obvious example, and a great use of generics, is the col-
lection classes. The
ArrayList class, for example, was written to hold, simply, Object. This means
objects lose their type when added to the
ArrayList and a cast is needed when accessing an element
of the
ArrayList. However, code that uses a generic version of the ArrayList can say “I want this
ArrayList to hold only Strings.” This adds additional type-safety to Java because, if anything other
than a
String is added to the collection, the compiler will catch it. This also means that a cast is no
longer needed when accessing elements — the compiler knows it only holds
Strings and will produce
an error if the elements are treated as anything other than a
String. Specifying String as the parame-
terized type is as easy as placing the type in angle brackets:
ArrayList<String> listOfStrings; // <TYPE_NAME> is new to the syntax
String stringObject;
listOfStrings = new ArrayList<String>(); // <TYPE_NAME> is new to the syntax
listOfStrings.add(new String(“Test string”)); // Can only pass in String objects
stringObject = listOfStrings.get(0); // no cast required
7
Chapter 1: Key Java Language Features and Libraries
05_777106 ch01.qxp 11/28/06 10:43 PM Page 7