Java 7 features.

There are lot of new useful java 7 features which are introduced in Jdk7 which looks very promising as it tries to make software developer's job more easy and save his/her time as well.

Now lets see which are those features or changes which are in this new release.

Java 7 features

1. Strings in switch statement!

Java 7 has introduced new type which can be used in Switch statement a String along with primitive type and enums. This is very good feature as lot of times we have Strings as key to match and we often work out if-else and equals for the check. But now we can directly use Strings in switch statement, so that we can use Switch functionality to fullest.

2. Use of diamond operator :

Diamond operator is one of the cool feature of Java 7 feature set. When we use generics and declare a map, then we need to specify the types on both the sides as shown below which was redundant, but in Java 7 we dont need to declare the types at right hand side.

Following code snippet demonstrates use of Diamond Operator - Java 7 features
      //Before java 7
      Map> objMap = new TreeMap> ();

     //From Java 7 new java 7 features
    Map> objMap = new TreeMap <> (); // Use of diamond operator

With the use of diamond operator, now we dont need a list of types to be specified while declaring the map.

3. Automatic management of resources:

This is my favorite one, its very troublesome and there are chances to forget to close the streams or readers or writers etc. Normally we use finally of try-catch block to close these resources. But in java 7 we dont need to worry about it, it will automatically manage the resources we dont need to explicitly close the resources.

To achieve this we need to use the resources as shown below

Following code snippet demonstrates effective management of resources - Java 7 features
   try (FileInputStream fis = new FileInputStream("java7features.txt") ) {
           System.out.println( );
   } catch (IOException e) {

Note two things in above example, first is there is no finally needed and we are not closing the file input stream, other important thing is as we are declaring the file input stream in try only, we are not assigning its value to null first and then creating new file input stream.
But lets see what happens under the table, the resources which should be closed automatically must implement the java.lang.AutoCloaseable interface. This interface contains close method which called by JVM once control comes out of try catch block.Lets move to other exciting java 7 features.

4. Multi exception handling improvements

Sometimes we need to handle lot of exceptions so for this we need to have multi catch statements for every exception we handle, but in java 7 its not the case anymore. You can add multiple exceptions using the pipe separator in catch block as shown below

Following code snippet demonstrates better Exception Handling - Java 7 features
try {
       //Doing some operations...
} catch (IOException | NullPointerException e) {

We can add multiple multi catch statements as well as shown below :

   try {
       //Doing some more operations...
   } catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
   } catch( IOException es )

5. Fork and Join

Now a days parallel computing has become necessary for heavy or complex operations. Java 7 has introduced a Fork and Join framework which distributes the work across multiple cores and join them after completion which is the final result. These tasks or work is divided into small tasks until these task dont need any divide and all these tasks carried out separately. These task use a pool and task are divided according to that.
The core classes which will be useful for fork and join are the ForkJoinPool and ForkJoinTask.

6. File change Notifications

The long awaited and very much useful feature of notifications when file change has also introduced in Java 7. When any file or directory changes the changes or the events are notified using the API WatchService. For this we need to create the WatchService first and then register the file or directory which we want to monitor to the WatchService. We can also set the which event we want to monitor such as create/ modify or delete etc.

7. JLayerPane

Java has introduced another great feature for swing UI developer which is a JLayeredPane. With use of JLayeredPane we can draw on the top of the component and handle the events without actually modifying the underlying component. This is a great feature because it can be used in many effects such as transition effects, blur effects or spotlight effects etc.

8. Underscores in numeric literals

This is another excellent usability java 7 feature for those who hates reading the long integers and group them. This feature allows user to add any number of underscores which can appear anywhere between the digits in numeric literal. As shown below we are separating group of digits in numeric literals which is improving the readability of the number.

       int big_number = 12_345_6789;

But there are some rules for adding underscores between digits.
1. Underscore must be in between digits not at the beginning or at the end.
2. If the numeric literal in floating point type then underscore must not be adjacent to a decimal point.
3. Underscore must not prior to F and L suffix and in the occurrence of string of digit where it is expected.\

9. Support for dynamically typed language.

In this feature extension to the JVM to support the dynamically typed language to increase the performance. For this invokedynamic is being introduced which used to incorporate non java requirements. In short this feature is to make running of dynamically typed languages efficient.

Hope you have liked these

Java 7 features

Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment


Post a Comment