What's new in Java 6.0 - called Mustang.

Though there are no significant changes at the Language Level, though Mustang comes with a bunch of enhancements in the other areas like Core, XML and Desktop. Most of the features are applicable both to J2SE and J2EE Platforms.

This article just highlights a few of the main language enhancements that Java 6 represents for the Java language and the developers that use it:

• Common Annotations
• Scripting in the Java Platform
• JDBC 4.0
• Monitoring and Management
• Managing the File System

Common Annotations:

The aim of having Common Annotations API in the Java Platform is to avoid applications defining their own Annotations which will result in having larger number of Duplicates. This JSR-250 is targeted to cover Annotations both in the Standard as well the Enterprise Environments. The packages that contain the annotations are javax.annotation and javax.annotation.security.

Scripting in the Java Platform:

Java 6 provides the Common Scripting Language Framework for integrating various Scripting Languages into the Java Platform. Most of the popular Scripting Languages like Java Script, PHP Script, Bean Shell Script and PNuts Script etc., can be seamlessly integrated with the Java Platform.

Support for Intercommunication between Scripting Languages and Java Programs is possible now because of this. It means that Scripting Language Code can access the Set of Java Libraries and Java Programs can directly embed Scripting Code. Java Applications can also have options for Compiling and Executing Scripts which will lead to good performance, provided the Scripting Engine supports this feature.

There are two core components of the Scripting engine namely:
• Language Bindings
• The Scripting API

For example:
You obtain a new ScriptEngine object from a ScriptEngineManager, as shown here:
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");

Each scripting language has its own unique identifier. The "js" here means you're dealing with JavaScript.

Now you can start having some fun. Interacting with a script is easy and intuitive. You can assign scripting variables using the put() method and evaluate the script using the eval() method,. which returns the most recently evaluated expression processed by the script. And that pretty much covers the essentials. Here's an example that puts it all together:

engine.put("cost", 1000);
String decision = (String) engine.eval(
"if ( cost >= 100){ " +
" decision = 'Ask the boss'; " +
"} else {" +
" decision = 'Buy it'; " +
"}");
assert ("Ask the boss".equals(decision));

You can do more than just pass variables to your scripts— you can also invoke Java classes from within your scripts. Using the importPackage() function enables you to import Java packages, as shown here:

engine.eval("importPackage(java.util); " +
"today = new Date(); " +
"print('Today is ' + today);");

Another cool feature is the Invocable interface, which lets you invoke a function by name within a script. This lets you write libraries in scripting languages, which you can use by calling key functions from your Java application. You just pass the name of the function you want to call, an array of Objects for the parameters, and you're done! Here's an example:

engine.eval("function calculateInsurancePremium(age) {...}");
Invocable invocable = (Invocable) engine;
Object result = invocable.invokeFunction("calculateInsurancePremium",
new Object[] {37});

You actually can do a fair bit more than what I've shown here. For example, you can pass a Reader object to the eval() method, which makes it easy to store scripts in external files, or bind several Java objects to JavaScript variables using a Map-like Binding object. You can also compile some scripting languages to speed up processing. But you probably get the idea that the integration with Java is smooth and well thought-out.

JDBC 4.0:

Java Database Connectivity allows Application Programs to interact with the Database to access the Relational Data. JDBC provides the Pluggable Architecture wherein any type of Java Compliant Drivers can be plugged-in even during the run-time. The JDBC API provides functionality to establish Connection to the back-end Database session which can execute the Queries to get the Results. The new version of JDBC that comes along with Mustang is JDBC 4.0. JDBC 4.0 is one of the areas that were great affected with the new set of features.
• No need for Class.forName("DriverName")
• Changes in Connection and Statement Interface
• Enhanced SQL Exception Handling

Monitoring and Management:

Java 6 has made some good progress in the field of application monitoring, management, and debugging. The Java Monitoring and Management Console, or JConsole, is well known to system administrators who deploy Java applications.

Java 6 enhances JConsole in several ways, making it both easier to use and more powerful. The graphical look has been improved; you can now monitor several applications in the same JConsole instance, and the summary screen has been redesigned. It now displays a graphical dashboard of the key statistics. You also can now export any graph data in CSV form for further analysis in a spreadsheet.

One great thing about application monitoring in Java 6 is that you don't need to do anything special to your application to use it. In Java 5, you needed to start any application that you wanted to monitor with a special command-line option (-Dcom.sun.management.jmxremote). In Java 6, you can monitor any application that is running in a Java 6 VM.

Java 6 comes with sophisticated thread-management and monitoring features as well. The Java 6 VM can now monitor applications for deadlocked threads involving object monitors and java.util.concurrent ownable synchronizers. If your application seems to be hanging, JConsole lets you check for deadlocks by clicking on the Detect Deadlock button in the Threads tab.
At a lower level, Java 6 helps resolve a common hard-to-isolate problem: the dreaded java.lang.OutOfMemoryError. In Java 6, an OutOfMemoryError will not just leave you guessing; it will print out a full stack trace so that you can have some idea of what might have caused the problem.

Managing the File System:


Java 6 gives you much finer control over your local file system. For example, it is now easy to find out how much free space is left on your hard drive. The java.io.File class has the following three new methods to determine the amount of space available (in bytes) on a given disk partition:

File homeDir = new File("/home/john");
System.out.println("Total space = " + homeDir.getTotalSpace());
System.out.println("Free space = " + homeDir.getFreeSpace());
System.out.println("Usable space = " + homeDir.getUsableSpace());

As their names would indicate, these methods return the total amount of disk space on the partition (getTotalSpace()), the amount of currently unallocated space (getFreeSpace()), and the amount of available space (getUsableSpace()), after taking into consideration OS-specific factors such as write permissions or other operating-system constraints. According to the documentation, getUsableSpace() is more accurate than getFreeSpace().

When I ran this code on my machine, it produced the following output:

Total space = 117050585088
Free space = 100983394304
Usuable space = 94941515776

File permissions are another area where Java 6 brings new enhancements. The java.io.File class now has a set of functions allowing you to set the readable, writable, and executable flags on files in your local file system, as you would with the Unix chmod command. For example, to set read-only access to a file, you could do the following:

File document = new File("document");
documentsDir.setReadable (true);
documentsDir.setWritable(false);
documentsDir.setExecutable (false);

This will set read-only access for the owner of the file. You can also modify the access rights for all users by setting the second parameter (ownerOnly) to false:

documentsDir.setReadable (true, false);
documentsDir.setWritable(false, false);
documentsDir.setExecutable (false, false);

Naturally, this will work only if the underlying operating system supports this level of file permissions.

Just a Few of Many

Java 6 offers many other new features that I haven't mentioned here, such as support for JAX-WS Web services and JAXB 2.0 XML binding, improvements in the Swing and AWT APIs, and language enhancements such as sorted sets and maps with bidirectional navigation. Try it out!