Compilation in J2SE 5.0


One of my aims is to make the examples portable, which means that they should compile and execute in J2SE 5.0 and J2SE 1.4. At the moment, May 2005, many Java users haven't upgraded to the latest version, and many PCs come with JRE 1.4 preinstalled.

As mentioned in the Preface, the main areas where I lose out because of this portability are in type-safe collections and the nanosecond time method, System.nanoTime( ). The Java 3D nanosecond timer is a good replacement for nanoTime( ). But what about type-safe collections?

What Is a Type-Safe Collection?

A type-safe collection is a generified collection declared with a type argument for its generic component. For example, this J2SE 1.4 code doesn't use generics:

     ArrayList al = new ArrayList( );     al.add(0, new Integer(42));     int num = ((Integer) al.get(0)).intValue( );

Collections without generic arguments are called raw types.

The following J2SE 5.0 code uses a generified ArrayList with an Integer type:

     ArrayList<Integer> al = new ArrayList<Integer>( );     al.add(0, new Integer(42));     int num = ((Integer) al.get(0)).intValue( );

Type safety means that the compiler can detect if the programmer tries to add a non-Integer object to the ArrayList. Poor coding like that would only be caught at runtime in J2SE 1.4, as a ClassCastException.

Generified collections can make use of J2SE 5.0's enhanced for loop, enumerations, and autoboxing. For example, the code snippet above can be revised to employ autoboxing and autounboxing:

     ArrayList<Integer> al = new ArrayList<Integer>( );     al.add(0, 42);     int num = al.get(0);

This is less verbose, and much easier to understand, debug, and maintain.

Dealing with Raw Types in J2SE 5.0

The J2SE 5.0 compiler will accept raw types (such as the ArrayList in the first code fragment in the previous section) but will issue warnings. This can be seen when the WormChase application is compiled with J2SE 5.0:

     >javac *.java     Note: Obstacles.java uses unchecked or unsafe operations.     Note: Recompile with -Xlint:unchecked for details.

The code has been compiled, but the unchecked warning indicates a raw type may be in Obstacles. Recompiling with the -Xlint argument leads to the following:

     >javac -Xlint:unchecked *.java     Obstacles.java:27: warning: [unchecked] unchecked call to add(E) as a member of     the raw type java.util.ArrayList       { boxes.add( new Rectangle(x,y, BOX_LENGTH, BOX_LENGTH));                  ^     1 warning

The problem is the boxes collection in the Obstacles class, specifically when a Rectangle object is added to it.

I've two options at this point: ignore the warning or fix it. Fixing it is straightforward, so I'll work through the stages here. Here is the original declaration of boxes in the Obstacles class:

     private ArrayList boxes;    // arraylist of Rectangle objects

This should be generified:

     private ArrayList<Rectangle> boxes;    // arraylist of Rectangle objects

The line that creates the boxes object must be changed from:

     boxes = new ArrayList( );

to:

     boxes = new ArrayList<Rectangle>( );

The program now compiles without any warnings.

A brisk introduction to the new features in J2SE 5.0 can be found in Java 1.5 Tiger: A Developer's Notebook by David Flanagan and Brett McLaughlin (O'Reilly). The issues involved with making type-safe collections are explored in more detail in: "Case Study: Converting to Java 1.5 Type-Safe Collections" by Wes Munsil in the Journal of Object Technology, September 2004 (http://www.jot.fm/issues/issue_2004_09/column1).



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net