Skip to Main Content
Killer Game Programming in Java
book

Killer Game Programming in Java

by Andrew Davison
May 2005
Intermediate to advanced content levelIntermediate to advanced
998 pages
26h
English
O'Reilly Media, Inc.
Content preview from Killer Game Programming in Java

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> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Game Development with LibGDX: From Beginner to Professional

Java Game Development with LibGDX: From Beginner to Professional

Lee Stemkoski

Publisher Resources

ISBN: 0596007302Supplemental ContentErrata Page