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 2

Understanding generics

Welcome to the second part of this three-part series on Sun Microsystems' latest release of the Java 2 Platform, Standard Edition (J2SE). To refresh your memory, Part 1 was a quick introduction to J2SE 1.5 and covered many new additions to the Java language: auto-boxing and -unboxing of primitives, enumerations, static imports, the enhanced "for" loop, variable method arguments, and the newly formatted input method. I devote Part 2 entirely to generics, Java's counterpart to templates in C++ and a similar facility (also called generics) in C# 2.0.

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



Why generics?

To understand the problem that generics (short for generic types) solve, let's look at the following code fragment:

// Declare Class A
class A
{
}
// Declare Class B
class B
{
}
// Somewhere in the program create a Vector 
Vector v = new Vector();
// Add an object of type A
v.add(new A());
// And sometime later get the object back
B b = (B) v.get(0);


The above code will compile fine but will throw a runtime exception (java.lang.ClassCastException) when you execute it. This is obviously a serious problem and should be caught as early as possible. Preferably, the above code fragment should not even compile.

Enter Java generics

Now, let's rewrite the above code fragment using generics:

// Class A and B declared previously
// Somewhere in the program create a Vector 
Vector<A> v = new Vector<A>();
// Add an object of type A
v.add(new A());
// And sometime later get the object back
B b = (B) v.get(0);


That looks similar to the first fragment, except for the code in the angle brackets. The angle brackets are the syntax for providing type parameters to parameterized types. I talk more about both parameterized types and type parameters in this article's later sections.

Even though the code change is minimal, its impact is far from small. This code fragment will not compile. Compiling this program with J2SE 1.5 (remember to use -source 1.5) will give you the following error:

inconvertible types
found   : A
required: B
        B b = (B) v.get(0);


In plain English, the compiler tells us that to be able to use the line B b = (B) v.get(0); legally, the vector must be parameterized (in the declaration) to accept objects of type B.

Diving into generics

Generics were first proposed for Java in early 2001, and (after three long years) they have finally made their way into J2SE 1.5. As you saw above, generics provide a giant leap into solving a long-standing problem in Java; implementing compile-time type safety. One of the first places you will encounter generics in J2SE 1.5 (and in C++ and C#) are in the collection classes, such as the Vector class we used above. Such classes are called parameterized types. If a type is parameterized, then its declaration (i.e., when you declare an instance of that type) can optionally take a type with which to parameterize it. For example, we already know that Vector is a parameterized type, and hence, the declaration of v (in the example above) is as follows:

1 | 2 | 3 |  Next >

Discuss

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

Subject Replies Last post
. Further confusions with Generics
By ihandler
6 11/02/06 05:49 AM
by Anonymous
. Disprove Tarak's arguments for generics
By Dimitar P.
4 10/29/06 08:43 AM
by Anonymous
. Taming Tiger, Part 2
By JavaWorldAdministrator
1 10/29/06 06:48 AM
by Anonymous
. Confusing generics
By Vignesh
2 10/29/06 04:51 AM
by Anonymous
. confusing "generic method"
By ertri
1 10/29/06 04:50 AM
by Anonymous
. still don't get it....
By bboett
0 10/29/06 12:21 AM
by Anonymous
. Confusing Generic method
By RIYAAG
2 10/29/06 12:20 AM
by Anonymous
. Flaw with generics
By Daniel Barbalace
2 10/05/06 08:49 AM
by Anonymous
. How generics could have been more readable
By Daniel Barbalace
4 10/05/06 08:48 AM
by Anonymous
. KOAjQ4Wg1M
By KOAjQ4Wg1M
0 02/25/06 02:31 PM
by Anonymous


Resources