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
for (TimerTask task : c)
task.cancel();
}
Note the Collection c is of object type Timer Task according to the
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:
Integer ii = (Integer) 66;
//Will be relaxed to the much more natural
Integer ii = 66;
C# programmers will get no arguments from this quarter as auto-boxing/unboxing reduces dumb compiler errors when boolean, int , double, float and other primitive Java types are used with their object counterparts.
4)enums - The first reaction is hohum, they have done enums finally catching up with C, C++, C#, etc.
enum Seasons { fall, winter, spring, summer;}
looks pretty straightforward. But each enum is treated as a class declaration in Java with no public constructor. So then very powerful methods and other functionality can be attached to an enum as in:
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.*;
import static java.awt.BorderLayout.*;
...
//The old Math references
xx=Math.sqrt(Math.PI);
//the new Math references
double xNeway = sqrt(PI);
//Note similar, simplified references to java.awt.BorderLayout constants
getContentPane().add(new JPanel(), CENTER);
This is another welcome usability improvement to Java.
6)varargs - allows for a varriable number of arguments to be passed to a method using the "..." syntax as show in the example below:
void argtest(Object ... args) {
for (int i=0;i < args.length; i++) {
System.out.println("The value is "+args[i]+"\n");
}
}
argtest("test", "data");
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
It would be a major mistake to characterize the 1.5 update as being primarily updates to streamline the syntax of the language. There are a number of other major changes to the Java class libraries and execution environ. For example, there are updates to the thread classes and concurrency utilities including adding of streamlined semaphore coding. The new metadata construct when combined with the reflection classes will allow program generation facilities to make much more "user-informed" decisions of what to do. The new JAXP XML facilities provide core XML platform support including XML 1.1 and Namespace, XML Schema. SAX 2.0.1. XSLT and the fast XLSTC compiler, and finally DOM Level 3. However, Web Services extension appear to be slated for a 1.5.x update. But it appears Simple Authentication and Security Layer (SASL) Java API may well make this version release. There are also new Java Platform profiling capabilities for the many new devices and platforms Java runs on and needs to be optimized for. There are a set of new Monitoring and Management Specification for the Java Virtual Machine which will help to manage and optimize runtime performance . In sum, Sun appears to be stocking Java with a full range of improvements that will make J2SE a very compelling upgrade; hopefully to go along with some of the rumored developmental tool improvements. The Java One Conference should be most interesting this year.
No comments:
Post a Comment