Appendix B. Retrofitting JDK 5.0 Code

This book uses JDK 5.0 features in many of its programs. This appendix summarizes the changes that you need to make to the sample programs so that they compile and run with JDK 1.4.

Enhanced for Loop

The enhanced for loop (or “for each” loop) must be turned into a traditional loop.

5.0

1.4

for (type variable : array){   body}

for (int i = 0; i < array.length; i++){   type variable = array[i];   body}

for (type variable : arrayList){   body}

for (int i = 0; i < arrayList.size(); i++){   type variable = (typearrayList.get(i);   body}

Generic Array Lists

You need to remove the type parameters and add casts to any get operations.

5.0

1.4

ArrayList<Type> arrayList = new ArrayList<Type>()

ArrayList arrayList = new ArrayList();

arrayList.get(i)

(Type) arrayList.get(i)

Autoboxing

Before JDK 5.0, the conversion between primitive types and their wrapper classes was not automatic. To convert from a primitive type to a wrapper object, add a constructor call.

5.0

1.4

Integer wrapper = n;

Integer wrapper = new Integer(n);

To convert a wrapper to a primitive type value, add calls to methods intValue, doubleValue, and so on.

5.0

1.4

int n = wrapper;

int n = wrapper.intValue();

Variable Parameter Lists

You need to bundle up the parameters into an array.

5.0

1.4

method(other params, p1, p2, p3)

method(other params, new Type[] { p1, p2, p3 })

Covariant Return Types

Prior to JDK 5.0, the return type could not change when overriding a method. It is now legal to use a subtype in the overriding method. A typical example is the clone method.

5.0

1.4

public Employee clone() { ... }
...
Employee cloned = e.clone();
public Object clone() { ... }
...
Employee cloned = (Employee) e.clone();

Static Import

Static import is not supported prior to JDK 5.0. Add the class name to the static feature.

5.0

1.4

import static java.lang.Math;
import static java.lang.System;
...
out.println(sqrt(PI));
System.out.println(Math.sqrt(Math.PI));

Console Input

Prior to JDK 5.0, there was no Scanner class. Use JOptionPane.showInputDialog instead.

5.0

1.4

Scanner in = new Scanner(System.in);System.out.print(prompt);int n = in.nextInt();double x = in.nextDouble();String s = in.nextLine();

String input = JOptionPane.showInputDialog(prompt);int n = Integer.parseInt(input);double x = Double.parseDouble(input);s = input;

Formatted Output

Prior to JDK 5.0, there was no printf method. Use NumberFormat.getNumberInstance instead.

5.0

1.4

System.out.printf("%8.2f", x);

NumberFormat formatter   = NumberFormat.getNumberInstance();formatter.setMinimumFractionDigits(2);formatter.setMaximumFractionDigits(2);String formatted = formatter.format(x);for (int i = formatted.length(); i < 8; i++)   System.out.print(" "); System.out.print(formatted);

Content Pane Delegation

Prior to JDK 5.0, the JFrame, JDialog, and JApplet classes did not delegate add and setLayout calls to the content pane. Note that this issue is only reported at run time.

5.0

1.4

add(component)

getContentPane().add(component)

setLayout(manager)

getContentPane().setLayout(manager)

Unicode Code Points

JDK 5.0 supports Unicode 4.0, in which the “supplementary” characters are encoded with two consecutive char values.

5.0

1.4

int cp = string.codePointAt(i)

char cp = string.charAt(i)

if (Character.isSupplementaryCharacter(cp))
   ...

omit—there are no supplementary characters prior to JDK 5.0

Building Strings

JDK 5.0 introduces a StringBuilder class whose methods are not synchronized, making it slightly more efficient than StringBuffer.

5.0

1.4

StringBuilder

StringBuffer

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset