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.
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 |
for (type variable : arrayList){ body} | for (int i = 0; i < arrayList.size(); i++){ type variable |
You need to remove the type parameters and add casts to any get
operations.
5.0 | 1.4 |
---|---|
|
|
arrayList | (Type) arrayList |
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 |
---|---|
|
|
To convert a wrapper to a primitive type value, add calls to methods intValue
, doubleValue
, and so on.
5.0 | 1.4 |
---|---|
|
|
You need to bundle up the parameters into an array.
5.0 | 1.4 |
---|---|
method(other params, p1, p2, p3) | method(other params, |
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 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)); |
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); |
Prior to JDK 5.0, there was no printf
method. Use NumberFormat.getNumberInstance
instead.
5.0 | 1.4 |
---|---|
| NumberFormat formatter = NumberFormat.getNumberInstance(); |
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 |
---|---|
|
|
|
|