October 25, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

5 More Notable Java 7 Changes

  • December 19, 2011
  • By Sridhar M S
  • Send Email »
  • More Articles »

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.


Tags: JDK, Java 7



Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel