| Java 1.5 - Tiger Beta available | |||||
Here are the 6 major changes to Java syntax and some of their implications. 1)Generics allows the type of contents of arrays and other collections to be specified as in the following: void cancelAll(Collection<TimerTask> c) {
for (TimerTask task : c)
task.cancel();
}
Note the Collection c is of object type Timer Task according to the <Generic>declaration.This
supposedly has the advantage that bad coding practices where users fail to
check for correct object types in collection classes can be avoided. We have never
liked generics and still do not for the hideously ugly and error prone syntax
that it can result
in. Supposedly reduced runtimes errors
are replaced by greatly increased compile time errors and less understanding of
what the code is doing. The following is much preferred to generics:
void cancelAll(Collection c) {
for (Object task : c)
if(task instanceof TimerTask)(TimerTask)task.cancel();
} This syntax requires an instanceof validity check followed by a cast. Developers worked so hard to get languages with collection classes and containers which could hold different object types; don't shrink now. And if anyone has seen C++ code with generics especially where the relational operators > or < are involved they will know the devil of error->prone non<->understandability that has to be paid when generics get unleashed. For a counter point of view check "Adding generics to the Java programming language" at the Sun site. 2)Enhanced for loop brings the popular foreach construct into Java and use with arrays and collection classes. We have already used it in the example above where for (Object task : c) should be read as "foreach Object task in the Collection c do the statement(s) below until all of the items in Collection c are retreived; then exit the loop". Foreach functionality in Perl and PHP have made the use of associative arrays and hashes very easy. Note the enhanced for works with arrays: int sum(int[] a) {
int result = 0;
for (int i : a) //Note our foreach usage here
result += i;
return result;
} 3)Auto-boxing/unboxing - C# coders really take Java
to task for lack of auto-boxing facilities. Thus the following: public enum Coin {
penny(1), nickel(5), dime(10), quarter(25);
Coin(int value) { this.value = value; }
private final int value;
public int value() { return value; }
}
Suddenly enums can return meaningful values and do more useful things. See
the following Java Community Process paper for
other intriguing uses of enums.5)import static - allows importing static member functions or constants from a class with the usual syntax but without having to inherit from the class. This can eliminate some tedious syntax repetitions as shown below: import static java.lang.Math.*; This is another welcome usability improvement to Java.
void argtest(Object ... args) {
for (int i=0;i < args.length; i++) {
This functionality allows the incorporation of the printf() formatted output to Java as in:
System.out.printf("name count\n");
System.out.printf("%s %5d\n", user,total);
In general, variable argument lists only through the command line has been a Java bane until now. Other Significant Changes | |||||
Top of Page Tutorials Home |