SniptoolsSniptools | Design & Technology Observations

RSS

Sun’s Java 1.5 beta

Apr 6th 2004
No Comments

Respond
Trackback

Some new features in Java 1.5, a beta version of which is up for grabs.

Sun has released the Java 1.5 beta 1 program. Read the release notes here.

Although most shops may not be seen scurrying to the latest version for quite some time, it’s never too early to start planning. Java 1.5 contains lots of new features and changes that will affect Java users across the board, but from a programmer’s perspective, Java 1.5 is a programmer’s release. Here are some of the new features and enhancements that are important to programmers.

Enum support

Enumerators have been missing from Java since the beginning. Programmers no longer have to use workarounds to get typesafe enumerators.

Autoboxing/unboxing

When calling a method that takes an int as an argument, you won’t have to do this:

takesInt(someInteger.intValue())

Instead, the compiler will do it for you.

Generics

By far, the most hyped feature of the language is generics. In a nutshell, generics will allow you to use typesafe collections. This is advantageous because you’ll get ClassCastExceptions at compile time instead of runtime.

A new for loop

Sun calls it the enhanced for loop. The enhancement is that the for loop now works with collections and arrays naturally.

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

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

Variable argument lists

Methods may now accept a variable number of arguments. This is achieved by declaring the method with ellipsis notation (three dots) between the type and argument name, like this: static void flexible(String … names).

System.out.printf

You can use printf-like formatting to create console output and strings just like your C-coder compaƱeros. The class that gets the work done is Formatter, which has a complementary class called Scanner that can parse strings using regular expressions.

Parametrized Types

In accordance to JSR 14, Sun will add parameterized types to the language. This is pretty useful. You can use types like:

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

A New “For” Loop

The 1.5 version will include a new for() loop for easily iterating through the elements of collections and arrays. Basically a foreach loop, but it just isn’t called that.

Miscellaneous Others

  • Small, but no less important, are enumerated constants!
  • varargs — methods can have variable numbers of arguments (as long as the variable part of the argument list are all of the same type)
  • Also, you can now import the static members of a class, such as the constants defined by an enumerated type or the methods of the java.lang.Math class.

I’ll add more as I discover them, in practice. No point regurgitating the huge specification request list. Now don’t just sit there, get your butt to the download site already!




This post is tagged

No Comments