JDK 1.5 Language Enhancements

Overview

  • Generics
  • Enhanced for loop
  • Autoboxing and auto-unboxing
  • Typesafe enumerated types (enums)
  • Variable arguments (varargs)
  • Static imports
  • Metadata – Annotations

One of the main considerations in creating J2SE 5.0 was the ease of use for programmers. This slide lists some of the main changes to the language specification:

  • Generics provides compile-time type checking and eliminate the need for casts. This serves to optimize collections of older Java versions.
  • Enhanced for-loops make it easy to traverse through collections while avoiding error-prone iterators.
  • Autoboxing and Auto-unboxing reduce the efforts previously required to cast primitive types back and forth. The compiler now does this for you.
  • Typesafe enums provide a way to define enumerated types that can be checked by the compiler for type safety.
  • Varargs allow a variable number of arguments to be passed to a method.
  • Static Imports simplify the task of importing constants and other static members in your code like when you are importing packages.
  • Metadata or annotations provide a standard way to annotate your code.

Generics

  • Generics provide a way for you to communicate the type of a collection to the compiler so that it can be checked.
  • The problem with using collections – Type safety:
    • To take an element out of a Collection, you must cast it
    • Besides being inconvenient, this is unsafe
    • The compiler does not check that your cast is the same as the collection’s type
    • The cast can fail at runtime
  • Solution – Type-safe collections:
    • With generics, you communicate the type of a collection to the compiler.
    • The compiler can then check that you have used the collection consistently.
    • When retrieving an element from the collection, the compiler performs the cast for you.
  • Your code becomes cleaner and more reliable

Enhanced for Loop

  • The for-each loop provides a simple syntax to iterate over collections and arrays:

    for ( Type x: collection ) {
        x.doSomething();
    }

  • Simplifies and speeds up the coding of iterators
  • Preserves all of the type safety while removing the remaining clutter

Autoboxing and Auto-Unboxing

  • You have to box and unbox primitive values (int, double, float, …) from the appropriate wrapper class (Integer, Double, Float, …) to put them into or get them out of collections.
  • Before Java 5, the developer was responsible for the boxing and unboxing:

    Integer grade = new Integer(10);
int g = grade.intValue();

  • This required boxing and unboxing is tedious and clutters up your code
  • With J2SE platform 5.0, the compiler automatically performs the boxing and unboxing:

    Integer grade = 10;
int g = grade;

  • The autoboxing and auto-unboxing feature automates the process, eliminating the clutter.

Typesafe Enums

  • With the J2SE platform 5.0, the Java language provides typesafe enums.
  • Java language typesafe enums resemble their C/C++ counterparts: enum Season { WINTER, SPRING, SUMMER, FALL }
  • In addition, the new Java language enum declaration defines a full-fledged class – an enum type

Varargs

  • In past releases, a method that took an arbitrary number of values required you to create an array and put the values into the array.
  • Multiple arguments must be passed in an array, but the varargs feature automates and hides the process.
  • Code sample: public static String format( String pattern, Object… arguments )
  • In the preceding example, arguments parameter is an array of Objects.

Static Imports

  • In order to access static members from other classes, it is necessary to qualify references with the class they came from: double r = Math.cos(Math.PI * theta);
  • The static import allows unqualified access to static members
  • Instead, the program imports the members, individually:

    import static java.lang.Math.PI;
or in a group:
    import static java.lang.Math.*;

  • With static import, the previous code becomes:

    double r = cos(PI * theta);

Metadata – Annotations

  • Repeated writing of boiler plate code – For example, boiler plate code associated with using Java API for XML-based RPC (JAX RPC)
  • The requirement for supporting side files – For example, BeanInfo files associated with JavaBeansTM architecture.
  • The introduction of situation-specific annotation mechanisms – For example, the transient modifier in the Java platform.
  • As of release 5.0, the J2SE platform has a general purpose annotation (also known as metadata)
  • Now you can define and use your own annotation types
  • The facility consists of:
    • A syntax for declaring annotation types
    • A syntax for annotating declarations
    • APIs for reading annotations
    • A class file representation for annotations
    • An annotation processing tool (apt)

Overview Generics Enhanced for loop Autoboxing and auto-unboxing Typesafe enumerated types (enums) Variable arguments (varargs) Static imports Metadata – Annotations One of the main considerations in creating J2SE 5.0 was the ease of use for programmers. This slide lists some of the main changes to the language specification: Generics provides compile-time type checking and eliminate…

Leave a Reply

Your email address will not be published. Required fields are marked *