Sun's Java 1.5 beta

Some new fea­tures in Java 1.5, a beta ver­sion of which is up for grabs.

Sun has released the Java 1.5 beta 1 pro­gram. Read the release notes here.

Although most shops may not be seen scur­ry­ing to the lat­est ver­sion for quite some time, it's never too early to start plan­ning. Java 1.5 con­tains lots of new fea­tures and changes that will affect Java users across the board, but from a programmer's per­spec­tive, Java 1.5 is a programmer's release. Here are some of the new fea­tures and enhance­ments that are impor­tant to programmers.

Enum sup­port

Enu­mer­a­tors have been miss­ing from Java since the begin­ning. Pro­gram­mers no longer have to use workarounds to get type­safe enumerators.

Autoboxing/unboxing

When call­ing a method that takes an int as an argu­ment, you won't have to do this:

takesInt(someInteger.intValue())

Instead, the com­piler will do it for you.

Gener­ics

By far, the most hyped fea­ture of the lan­guage is gener­ics. In a nut­shell, gener­ics will allow you to use type­safe col­lec­tions. This is advan­ta­geous because you'll get Class­Cas­tEx­cep­tions at com­pile time instead of runtime.

A new for loop

Sun calls it the enhanced for loop. The enhance­ment is that the for loop now works with col­lec­tions and arrays naturally.

for ( Object o : someCollection ) {
System.out.println(o);
}

for ( String s : someStringArray ) {
System.out.println(s);
}

Vari­able argu­ment lists

Meth­ods may now accept a vari­able num­ber of argu­ments. This is achieved by declar­ing the method with ellip­sis nota­tion (three dots) between the type and argu­ment name, like this: sta­tic void flexible(String … names).

System.out.printf

You can use printf-like for­mat­ting to cre­ate con­sole out­put and strings just like your C-coder compañeros. The class that gets the work done is For­mat­ter, which has a com­ple­men­tary class called Scan­ner that can parse strings using reg­u­lar expressions.

Param­e­trized Types

In accor­dance to JSR 14, Sun will add para­me­ter­ized types to the lan­guage. This is pretty use­ful. You can use types like:

Map<String,Integer> map = new HashMap<String,Integer>

A New "For" Loop

The 1.5 ver­sion will include a new for() loop for eas­ily iter­at­ing through the ele­ments of col­lec­tions and arrays. Basi­cally a foreach loop, but it just isn't called that.

Mis­cel­la­neous Others

  • Small, but no less impor­tant, are enu­mer­ated constants!
  • varargs — meth­ods can have vari­able num­bers of argu­ments (as long as the vari­able part of the argu­ment list are all of the same type)
  • Also, you can now import the sta­tic mem­bers of a class, such as the con­stants defined by an enu­mer­ated type or the meth­ods of the java.lang.Math class.

I'll add more as I dis­cover them, in prac­tice. No point regur­gi­tat­ing the huge spec­i­fi­ca­tion request list. Now don't just sit there, get your butt to the down­load site already!

0 comments
Submit comment