Intel AS/400 RISC Server User Manual

Page 139

Advertising
background image

int i = 0;
try {
while (true) {
System.out.println (arr[i++]);
}
} catch (ArrayOutOfBoundsException e) {
// Reached the end of the array....exit
}
}

Instead, the above procedure should be written as:

public void goodPrintArray (int arr[]) {
int len = arr.length;
for (int i = 0; i < len; i++) {
System.out.println (arr[i]);
}

}

In the “bad” version of this code, an exception will always be thrown (and caught) in every execution
of the method. In the “good” version, most calls to the method will not result in an exception.
However, if you passed “null” to the method, it would throw a NullPointerException. Since this is
probably not something that would normally happen, an exception may be appropriate in this case.
(On the other hand, if you expect that null will be passed to this method frequently, it may be a good
idea to handle it specifically rather than throwing an exception.)

y

Use static final when creating constants
When data is invariant, declare it as static final. For example here are two array initializations:

class test1 {
int myarray[] =
{ 1,2,3,4,5,6,7,8,9,10,
2,3,4,5,6,7,8,9,10,11,
3,4,5,6,7,8,9,10,11,12,
4,5,6,7,8,9,10,11,12,13,
5,6,7,8,9,10,11,12,13,14 };
}

class test2 {
static final int myarray2[] =
{ 1,2,3,4,5,6,7,8,9,10,
2,3,4,5,6,7,8,9,10,11,
3,4,5,6,7,8,9,10,11,12,
4,5,6,7,8,9,10,11,12,13,
5,6,7,8,9,10,11,12,13,14 };
}

Since the array myarray2 in class test2 is defined as static, there is only one myarray2 array for all the
many creations of the test2 object. In the case of the test1 class, there is an array myarray for each
test1 instance. The use of final ensures that the array cannot be changed, making it safe to use from
multiple threads.

Java i5/OS Database Access Tips

y

Use the native JDBC driver
There are two i5/OS JDBC drivers that may be used to access local data: the Native driver (using a
JDBC URL "jdbc:db2:system-name") and the Toolbox driver (with a JDBC URL
"jdbc:as400:system-name"). The native JDBC driver is optimized for local database access, and
gives the best performance when accessing the database on the same system as your Java

IBM i 6.1 Performance Capabilities Reference - January/April/October 2008

©

Copyright IBM Corp. 2008

Chapter 7 - Java Performance

139

Advertising
This manual is related to the following products: