Friday, October 08, 2004

Java 5 features

The new features in Java 5 can roughly be organized into four categories: enhanced language features, Virtual Machine upgrades, library updates, and desktop (UI) enhancements.

Language Features
There is a long list of language features that could be mentioned, but we will only take time to address some of the most significant developments.
Enumerated types – An enumerated type is a datatype containing a fixed set of constant values. As implemented in J2SE 5.0, enums are object-oriented (users can define methods and fields) and typesafe.

Formatted I/O – Console output has been enhanced by implementing printf-style formatters, enabling many legacy C applications to be ported without changing the expected output format. On the input side of the house, the Scanner API has been provided to offer developers a more robust mechanism for reading in data types rather than simply parsing strings from buffered System.in calls.

Generic types – Perhaps the most significant enhancements made to the Java language revolve around the introduction of generic types. Gone are the days when Java developers must toil and sweat to cast objects up and down the type hierarchy. With generic types, a collection’s type can be specified at compile time to permit compile type checks and ease development by providing implicit type casting. Additionally, primitive types are now able to automatically convert (autoboxing/unboxing) between the primitive type and its corresponding Object wrapper (int <----> Integer, boolean <----> Boolean, char <----> Char, etc.).

Metadata – J2SE 5.0 now supports the inclusion of metadata related to Java classes, interfaces, fields, and methods. Development tools are the intended consumers for this metadata to support the automated insertion of additional source code, debugging functionality, and creation/modification of external XML files or configuration files to support application runtime and deployment.

Varargs – With J2SE 5.0, support for passing a variable number of arguments into a method has been added. By using a special notation (…) in the method signature’s parameter list, an ad hoc Object array can be created.

Java Library

There are two new Java libraries, and a host of updates to existing Java libraries. The new libraries are the Java Concurrency Utilities (JSR-166) which provide advanced multithreading controls, and the JVM Profiling API (JSR-163), which is a more powerful native profiling API called JVMTI.
The concurrency utilities provide advanced, high-level multithreading capabilities such as thread-safe queues, a thread task framework through executors, locks (including semaphores), timers, and various other synchronization primitives. For more details, explore the JavaDocs under java.lang.concurrent.
The JVMTI implementation includes Java Programming Language Instrumentation Services (JPLIS) which function at the bytecode level to enable analysis tools to add profiling, monitoring, and debugging where it is needed. This technique provides great flexibility and limits the interference of profiling and monitoring tools on a currently executing JVM. For more details, explore the JavaDocs under java.lang.instrument. A wide range of other existing Java libraries have received important updates, including the Utility package, Networking, Security, RMI, JDBC, and the CORBA libraries.

Desktop (UI)

J2SE 5.0 has taken some significant strides to address the traditionally slow startup time and heavy-weight memory footprint that has traditionally plagued Java desktop applications. The new version boasts enhanced font capabilities (especially to support localization and internationalization), many updates to Java Sound (broader platform support, optimized direct audio access, an improved and real-time Sequencer, etc.), hardware-accelerated rendering for Java 2D using OpenGL drivers, and even a new skin changing look and feel (Synth) and Theme (Ocean) for Swing.

Virtual Machine

Error Handling – Although the improvements are simple, the value is tremendous. Java developers now have the ability to capture stack traces as String objects and use them however they need to using the getStackTrace and Thread.getAllStackTraces, rather than being forced to send Stack Trace information to the console. Additionally, the HotSpot JVM has been updated to include a fatal error handler for running a user-supplied script or program if the JVM dies. Finally, the HotSpot JVM has been given a serviceability agent connector that permits monitoring and debugging tools to connect to a hung JVM or core file and extract relevant diagnostic data.

Management and Monitoring – From instrumentation (JPLIS) to Java Management Extensions (JMX) support, to support of remote access protocols and SNMP tools, management and monitoring have been greatly enhanced with J2SE 5.0. All of this is ready to use out of the box, supporting local and remote management and monitoring of a running JVM.

Performance and Scalability – A great deal of attention has been given to the subject of performance in this latest release of the core Java platform. Java 5 starts more quickly, leaves a smaller memory footprint, and offers enhanced scalability by enabling multiple JVMs to share read-only data. One really interesting enhancement is the ability to designate the role of a JVM as either a ‘client’ or a ‘server’. The JVM then automatically tunes itself to provide the optimum environment for the specified role, delivering enhanced performance, garbage collection, and thread prioritization.

Analysis of Java 5

Overall, J2SE 5.0 has really aimed to make developing Java software easier. From language features that save time and effort (generics, formatted I/O, metadata, varargs, etc.), to enhanced error handling, JVM management, performance and scalability, and many Java library updates to ease common developer headaches.
With the inclusion of generics, overhauled JVM, and emphasis upon performance and scalability of the platform, the Sun engineering team addressed all the major items on my list of desired improvements to the Java platform. The only other thing that I really wished I had seen with this release was an enhanced Java Reference API to support more object-oriented caching capabilities within the platform straight out of the box (rather than relying upon this value-add from external vendors).

Conclusion

Java has come a long way over the past nine years. The strides made in this new release of the Java platform will no doubt be significant for the viability of the platform. It is critical that Sun shows the industry that Java is still an innovative platform that is robust enough and flexible enough to meet the ever-changing climate of the Information Technology sector. If the industry takes a close look at Java 5, I believe they will see that this is, in fact, the case. Go Tiger – 1, 2, …5!

Taming Tiger: Formatted output - Let there be printf

The original plan for the 1.4 release J2SE included support for formatted output. Probably due to time constraints and the fact that the feature wasn't a release driver, the capabilities were left out of the release. Now with Tiger, there is built in support for printing with format strings.

For those who grew up with Java programming and never touched C or those who haven't lived in a C world for some time, format strings are those funky text strings that specify output characteristics for a bunch of variables. Instead of just concatenating strings together with the plus sign (as in firstName + " " + lastName), you provide a single string to describe the output and provide the arguments to fill the placeholders in that string at the end of the method call: String s = String.format("%1$s %2$s", firstName, lastName).

Formatter class
First, let's look at the new java.util.Formatter class. You probably won't use this class directly much, but it provides the guts for the formatting you'll be doing. In the Javadoc for this class, you'll find a table describing the supported formatting options. These options range from something like %7.4f for specifying the precision and width of a floating point number to %tT for formatting a time to %3$s for formatting the third argument.

Using Formatter to format output involves two steps: creating an Appendable object to store the output and using the format() method to put formatted content into that object. Here's a list of implementers for the Appendable interface:

BufferedWriter
CharArrayWriter
CharBuffer
FileWriter
FilterWriter
LogStream
OutputStreamWriter
PipedWriter
PrintStream
PrintWriter
StringBuffer
StringBuilder
StringWriter
Writer
An object implementing this interface can be used as the destination when using a Formatter class by passing the object into the Formatter constructor. Most of these classes should look familiar, except for the StringBuilder class. StringBuilder is nearly identical to the StringBuffer class, with one big exception: It isn't thread safe. If you know you are going to build up a string in a single thread, use StringBuilder. If the building can cross thread bounds, use StringBuffer. Listing 1 shows how you'd typically start using Formatter:

Listing 1. Typical formatter usage

StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb, Locale.US);



After creating a Formatter class, you call its format() method with format strings and arguments. If you need to use a different Locale than that sent into the constructor for part of the formatted output, you can also pass in a Locale object to the format() method. Listing 2 shows the two varieties of format():

Listing 2. format() methods of Formatter

public Formatter format(String format,
Object... args)
public Formatter format(Locale l,
String format,
Object... args)



If you want to get the value of Pi to 10 digits of precision, the code in Listing 3 will put that value into the StringBuilder and print the output. Printing the formatter object will display the content of the Appendable object.

Listing 3. Demonstrating a Formatter

import java.util.Locale;
import java.util.Formatter;

public class Build {
public static void main(String args[]) {
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb, Locale.US);
formatter.format("PI = %12.10f", Math.PI);
System.out.println(formatter);
}
}



Don't forget to compile with the -source 1.5 option or the compiler won't recognize the variable argument list. Because formatting output and sending it to the console are common tasks, there are conveniences available to this behavior. We'll look at those next.

PrintStream support
The PrintStream class contains the definition of the common System.out and System.err objects for writing to standard output and standard error, respectively. Introduced in Tiger are two new constructors (for going straight to a file) and six methods for formatting support (three sets of pairs). The first pair is different versions of the append() method. This pair implements the new java.lang.Appendable interface. You would typically not call these methods directly. The ones you would call directly are format() and printf(), where the printf() versions are just convenience wrappers for the format() versions, as shown in Listing 4:

Listing 4. PrintStream.format methods

public PrintStream format(String format,
Object... args)
public PrintStream format(Locale l,
String format,
Object... args)



Keep in mind the new variable argument support, which is designated by the ... shown in Listing 4 above.

Listing 5 demonstrates the use of the format() method of PrintStream to print today's date:

Listing 5. Example PrintStream.format usage
/
import java.util.Calendar;

public class Now {
public static void main(String args[]) {
System.out.format(
"Today is %1$tB %1$te, %1$tY.",
Calendar.getInstance()
);
}
}



The output from running this program is Today is April 2, 2004., although the actual output depends on the date you run the program. The %1$tB formatting string in the code above tells the program to use the first argument and print out the full month name for the date object. The %1$te formatting string means to display the day of the month and the %1$tY formatting string is for the four-digit year. Other options for printing dates and times are shown in the Javadoc for the Formatter object.

String support
The String class has two new static format() methods that work similarly to their printf() equivalents. Send a format string and arguments (with a possible Locale) and use what is specified in the format string to convert the arguments. In the case of the String version of the method, the results are sent back as a String object, instead of going through the stream. These methods aren't overly spectacular, but they allow you to avoid using the Formatter object directly and creating an intermediate StringBuilder.

Formatting arbitrary objects
Everything you've seen so far describes how to use the new formatting capabilities to format existing objects and primitive types. If you want to provide support for using your own objects with Formatter, that's where the Formattable interface comes into play. By implementing the single formatTo() method shown in Listing 6 in your own class, you can use your own classes with format strings:

Listing 6. Formattable interface

void formatTo(Formatter formatter,
int flags,
Integer width,
Integer precision)



Listing 7 demonstrates the use of the Formattable interface by providing a simple class with a name property. That name is displayed in the output, with support for controlling the width of the output and the justification.

Listing 7. Example formattable usage

import java.util.Locale;
import java.util.Formatter;
import java.util.Formattable;

public class MyObject implements Formattable {
String name;
public MyObject(String name) {
this.name = name;
}
public void formatTo(
Formatter fmt,
int f,
Integer width,
Integer precision) {

StringBuilder sb = new StringBuilder();
if (precision == null) {
// no max width
sb.append(name);
} else if (name.length() < precision) {
sb.append(name);
} else {
sb.append(name.substring(0, precision - 1)).append('*');
}

// apply width and justification
if ((width != null) && (sb.length() < width)) {
for (int i = 0, n=sb.length(); i < width - n; i++) {
if ((f & Formattable.LEFT_JUSTIFY) == Formattable.LEFT_JUSTIFY) {
sb.append(' ');
} else {
sb.insert(0, ' ');
}
}
}
fmt.format(sb.toString());
}

public static void main(String args[]) {
MyObject my1 = new MyObject("John");
MyObject my2 = new MyObject("Really Long Name");
// First / Using toString()
System.out.println("First Object : " + my1);
// Second / Using Formatter
System.out.format("First Object : '%s'\n", my1);
// Second / Using Formatter
System.out.format("Second Object: '%s'\n", my2);
// Second / Using Formatter with width
System.out.format("Second Object: '%10.5s'\n", my2);
// Second / Using Formatter with width and left justification
System.out.format("Second Object: '%-10.5s'\n", my2);
}
}



Running this program produces the output in Listing 8. The first two lines demonstrate the difference between using toString and Formatter. The last three show options for width and justification control.

Listing 9. Example formattable output

First Object : MyObject@10b62c9
First Object : 'John'
Second Object: 'Really Long Name'
Second Object: ' Real*'
Second Object: 'Real* '



Conclusion
Getting a grasp of all the formatting options available with Formatter will take a little time, unless you are familiar with them from the C world. Some minor differences exist, but for the most part the behaviors are very similar. One key difference with the Java platform is that when the formatting string is invalid, an exception will be thrown.

Be sure to take a long look at the formatting strings available, as shown in the Formatter class Javadoc. When creating your own custom classes, not only should you provide a toString() implementation, but implementing the Formattable interface will typically be beneficial.