Back to the Java For Students site

J2SE 5.0

 

In mid 2004, Sun released a major new version of Java and its libraries, named Java 2 Platform Standard Edition 5.0 or J2SE 5.0.

 

This web page explains the impact of this release on Java for Students, 4th edition. We only discuss those features of the new release that might impact the book. It turns out that the impact is very minor.

 

It is important to realize that all the programs and all the ideas in the 4th edition of Java for Students work perfectly with J2SE 5.0.

 

We now look at:

 

·        importing static variables and methods

·        generics

·        printf and scanf

 

Enhanced for loop

 

An additional for loop is provided as a convenient shortcut in some cases. It is useful when you are processing all the elements in a collection such as an array list or an array.

 

Here is an example of using the for statement to concatenate all the elements in an array of strings:

 

String concatenated(String[] array) {

    String result = "";

    for (String s : array) {

        result = result + s;

    }

    return result;

}

 

Notice the use of the colon to mean "in". The for statement can be read as "for each s in array". The loop provides all the successive elements in the array (s in this example), but not the array index.

 

This enhanced for loop is no good if you need to do something special to a particular element in the collection - for example, delete, replace an item. As can be seen in the above example, the index is not available within the loop. In general, the iterator (the value that identifies a particular element) is unavailable in the loop.

 

Chapter 12 on array lists

 

We could add a discussion of the enhanced for loop here. It would act as a shortcut. But it tends to hide what is actually going on - that is the use of an index.

 

Chapter 13 on arrays

 

Same comments as on chapter 12. But additionally, some programs explicitly need the index value, which is unavailable, so the enhanced for would be unsuitable.

 

Chapter 25 on Polymorphism

 

Using the enhanced for would make the central program more elegant in its use of an array list. The central loop is currently:

 

        for (int s = 0; s < group.size(); s++) {

            Object item = group.get(s);

            Shape shape = (Shape) item;

            shape.display(paper);

        }

 

And it would become:

 

        for (Object item : group) {

            Shape shape = (Shape) item;

            shape.display(paper);

        }

 

OK - it is neater.

 

Our plan for the next edition would be to introduce the enhanced for as an adjunct to the ordinary for.

 

Autoboxing and Unboxing

 

In older versions of Java, you can’t put an int (or some other primitive value) into a collection, for example an array list. Collections can only hold objects, so you have to box primitive values into the appropriate wrapper class (which is Integer in the case of int). When you take the object out of the collection, you get the Integer that you put in; if you need an int, you must unbox the Integer using the intValue method. All of this boxing and unboxing clutters up the code. Autoboxing and unboxing performs these conversions automatically, eliminating the pain and the clutter. So you can largely ignore the distinction between int and Integer.

We think that this hiding of conversions is potentially confusing for students. In the book we emphasize strong typing, but this blows it away. So our plan is not to discuss this topic in the book.

 

importing static variables and methods

 

In order to access static members, it is necessary to qualify references with the class they came from. For example, you must say:

 

double r = Math.cos(Math.PI * theta);

 

The static import construct allows unqualified access to static members without inheriting from the type containing the static members. Instead, the program imports the members, either individually:

import static java.lang.Math.PI;

 

or all together:

 

import static java.lang.Math.*;

 

Notice the additional word static in these statements.

 

Once the static members have been imported, they may be used without qualification like this:

 

double r = cos(PI * theta);

The static import declaration is analogous to the normal import declaration. Where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification. The guidance on the Sun web site is to use this feature sparingly

Now we agree that using static variables and methods is confusing for students. But the change to the import is equally difficult to follow. We have no plan to discuss this in the book.

Generics

 

As things were, you cannot create a collection of String objects or a collection of any other class. All collections, including array lists consist of objects of type Object. So when you remove something from a collection, you have to convert (cast) it to the type of the object that it is.

 

For example, in chapter  25 on polymorphism, the core of the central program is currently:

 

    public void actionPerformed(ActionEvent event) {

        Graphics paper = panel.getGraphics();

 

        Circle circle = new Circle(20, 20);

        Square square = new Square(80, 80);

        ArrayList group = new ArrayList();

        group.add(circle);

        group.add(square);

 

        paper.setColor(Color.white);

        paper.fillRect(0, 0, 150, 150);

        paper.setColor(Color.black);

 

        for (int s = 0; s < group.size(); s++) {

            Object item = group.get(s);

            Shape shape = (Shape) item;

            shape.display(paper);

        }

    }

 

Notice the use of the (Shape) cast operator when an item is removed from the array list.

 

Generics allow you to create an array list that only holds the type you declare. Therefore when an item is removed, there is no need to cast it. Also a ClassCastException is prevented by the compile time checking. This all gives programs increased readability and robustness

 

With generics plus the enhanced for, the above example becomes:

 

    public void actionPerformed(ActionEvent event) {

        Graphics paper = panel.getGraphics();

 

        Circle circle = new Circle(20, 20);

        Square square = new Square(80, 80);

        ArrayList <Shape> group = new ArrayList();

        group.add(circle);

        group.add(square);

 

        paper.setColor(Color.white);

        paper.fillRect(0, 0, 150, 150);

        paper.setColor(Color.black);

 

        for (Object item : group) {

            item.display(paper);

        }

    }

 

which, of course is much neater and more elegant, mainly because it avoids casting.

 

For the next edition we plan to do generics from the start when we introduce data structures (array lists). So that every array list will hold information of a particular type.

 

printf and scanf

 

printf is a way of formatting numbers for output. It seems to have been included in Java to look like a similar facility in C. There is a new class called Formatter to do this. Also the class String has a new class method called format that does similar things. There is also a new class Scanner that scans input text.

 

One problem with introducing these is that they make use of methods with a variable number of parameters, which is another 5.0 feature that we do not plan to introduce.

 

In the current book, we explain how to use the formatting features of the DecimalFormat class, page 210 in chapter 11 on calculations. We explain how to use the StringTokenizer class to scan input text, page 284 in chapter 15 on strings.

 

We think that our treatment is perfectly adequate, so we do not plan to describe these new facilities.