Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Taming Tiger, Part 1

An introduction to Java 1.5

Welcome to the first of a three-part series on Sun Microsystems' latest release of the Java 2 Platform, Standard Edition (J2SE). J2SE 1.5—code-named Tiger—is the most significant revision to the Java language since its origination. With version 1.5, Java has been extended with several new features, and my goal with this series is to introduce you to the most important features.

Each article presents a detailed discussion of a handful of distinct features and provides practical examples of how they can be used in the real world. Part 1 is a quick introduction to J2SE 1.5 and covers many new additions. Part 2 will be devoted to generics, Java's counterpart to templates in C++ and a similar facility (also called generics) in C# 2.0. Part 3 focuses exclusively on J2SE 1.5's new metadata facility called annotations, which allow programmers to decorate Java code with their own attributes. These custom attributes can be used for code documentation, code generation, and, during runtime, to provide special services such as enhanced business-level security or special business logic.

Read the whole series: "Taming Tiger," Tarak Modi (JavaWorld):



As I mentioned before, J2SE 1.5 represents the largest overhaul of Java since its inception and contains many new and exciting features. The additions provide developers with a host of new capabilities. For example, the addition of enumerations and generics provides Java with compile-time type safety. That means programming or logic errors are caught during program compilation instead of during runtime. This may not seem to be a great benefit on the surface, especially for smaller projects. However, as your code's complexity increases and your project grows larger, it becomes more likely that parts of a program may not have been unit tested (I know this never happens on your project!), and after three months of smooth sailing in production, you answer a call at 2 a.m. that informs you of a java.lang.ClassCastException. Compile-time type safety catches all these errors during program compilation.

In this article, I discuss some of the new language features introduced in J2SE 1.5, including autoboxing and unboxing of primitives, the enhanced for loop, variable method arguments (varargs), the much anticipated enumerations, static imports, and the new formatted input method printf (a concept borrowed from C and C++).

All of these features are part of Java Specification Request 201. Without further ado, let's start with the first feature—autoboxing and unboxing.

Autoboxing and unboxing

Consider the following code fragment:

// A list that stores numbers
// Generics will make this obvious...
List numbers = new ArrayList();
numbers.add(89);


Prior to version 1.5, this code would not compile since ArrayList's add method expects an object. To make it work, you must modify the second line as follows:

   numbers.add(new Integer(89));


That seems unnecessary and, with J2SE 1.5, it is. With J2SE 1.5, the compiler automatically adds the code to convert the integer value (such as 89) into the proper class (Integer in our example). That is called boxing. The opposite process of converting an object (such as of type Integer) into a value (such as an int) is called unboxing. Boxing and unboxing eliminate the (frequent and often burdensome) need to explicitly convert data of primitive type to reference and vice versa. Such explicit required conversions are verbose and induce clutter in the program text. The following table shows the rules for boxing and unboxing.

Primitive type Reference type
boolean Boolean
byte Byte
double Double
short Short
int Integer
long Long
float Float


The enhanced for loop

A common programming task is iterating over the elements of a collection or an array. An example is shown in the code fragment below:

1 | 2 | 3 | 4 |  Next >

Discuss

Start a new discussion or jump into one of the threads below:

Subject Replies Last post
. No to 'C' or 'C++' style...
By Anonymous
5 10/29/06 06:43 AM
by Anonymous
. Taming Tiger, Part 1
By JavaWorldAdministrator
( Pages 1 2 all )
21 10/29/06 04:50 AM
by Anonymous
. General
By Anonymous
2 10/29/06 04:46 AM
by Anonymous
. as long it is backwards compatible i dont care
By uzhunthu_vadai
1 10/29/06 01:23 AM
by Anonymous
. No changes to VM I hope.
By Anonymous
2 10/29/06 01:21 AM
by Anonymous
. A few steps forward and a whole generation backward
By Anonymous
5 10/29/06 12:20 AM
by Anonymous
. "final" allowed
By Anonymous
1 10/05/06 10:19 AM
by Anonymous
. great review
By levani
0 10/05/06 05:36 AM
by Anonymous
. Java will be as complex as C++ soon!
By stevech
4 10/05/06 05:35 AM
by Anonymous
. Extending enumerations?
By Anonymous
3 10/04/06 09:06 AM
by Anonymous


Resources