http://www.developer.com/java/project-coin-languages-changes.html

Back to article

5 More Notable Java 7 Changes


December 19, 2011

Continuing the exploration of Java language features in JDK7 which I began in my previous installment on Project Coin, I will discuss the following Java 7 enhancements:

  • Improved type interface for generic instance creation
  • Simplified varargs method invocation
  • NIO.2 improvements
  • Rich GUI deployment
  • Sockets Direct Protocol

This list is by no means complete; it serves only as a compilation of the features I find most notable.

Improved Type Interface for Generic Instance Creation

This is a tiny change when compared with other, exhaustive Java 7 language changes. Having said that, improved type interface for generic instance creation makes a big difference in the way Java code will be written.

For example, consider the following declaration for a regionMap data structure, which dictates that the structure hold a key value pair with key as Integer and value as a List of String type.

Map<Integer, List<String>> regionMap = new HashMap<Integer, List<String>>();

Java code can look complex unless you are comfortable using generics. Generics have been considered a solution for most of the data integrity issues that developers come across typically during deployment rather than during development. Generics promote type-safety and eliminates unnecessary runtime issues by ensuring the appropriate data type in compilation.

Using JDK 7, the above line of code can be written in a simpler form.

Map<Integer, List<String>> regionMap = new HashMap<>();

The compiler has enough information about the declaration of the variable regionMap to interpret the specific data-types on the right side. This will be used to map the correct data-types, thus making the code more readable and less error prone.

Simplified Varargs Method Invocation

This is an enhancement to the way generics are used to some extent. Simplified varargs method invocation presents a workable solution to the generics limitation of not being able to create arrays that are generic in nature, as shown in the following example:

public static List<String>[] getSingleList() {
List<String> firstList = Arrays.asList("one", "two", "three");
List<String> secondList = Arrays.asList("four", "five", "six");
    
return new List<String>[]{firstList, secondList};  
//The above line will fail during compilation.
}

There has been a change request that adds more clarity on this.

NIO.2 Improvements Over NIO

Developers who use NIO can now use NIO.2 to access the file system. NIO.2 is enhanced with the java.nio.file package and its related package java.nio.file.attribute, which make it easy to work across operating systems or multi-file systems. A detailed tutorial is available on oracle.com.

The java.nio.file package has many classes and interfaces such as Path, Paths, FileSystem, FileSystems, and so on. Utility methods such as delete (e.g. delete(argPath) and deleteIfExists(argPath) in the Files class) were also added to avoid unnecessary problems.

However, existing code needs an overhaul to adapt to NIO.2. Developers will have to weigh the effort against the benefits of adopting NIO.2 for future portability and easy developmental support.

Rich GUI Deployment in Java 7

Those using Java Web Start for application launches can employ JDK 7 enhancements in the JNLP file syntax.

  • The OS attribute in the information and resources elements can now contain a specific version of Windows such as Windows Vista or Windows 7.
  • The codebase attribute can be ignored.
  • You can now embed a JNLP file into an HTML page.

A new class, JLayer, also has been added to enhance the capabilities of Java Swing. JLayer is used primarily to decorate Swing components. Oracle.com has a detailed JLayer tutorial for reference.

Other Swing changes include support for non-rectangular shapes and transparent windows, which make GUIs richer and more intuitive for end users.

Sockets Direct Protocol (SDP)

The Sockets Direct Protocol (SDP) provides access to high performance network connections, such as that provided by InfiniBand. Transferring data across networks quickly and efficiently is a requirement in high performance computing systems, where the conventional approach of transferring data using sockets is of little or no help. SDP is ideal in such environments and the throughput can be real-time.

Used effectively, these and all the other Java 7 changes/enhancements will make Java developers more productive.

Sitemap | Contact Us