Java 7 is Here!


Its finally here, the much awaited JAVA 7! After a wait of nearly 5 years, Oracle has released the latest installation of the Developer kit for the highly popular platform, Java! The new version still has the silly version number of 1.7.0 but is more popularly known as JDK 7 as in the case of JDK 6. JDK 7 brings some changes to the language features of Java and also some changes to the core systems and the VM as well. Id like to point out some of the new language features of Java 7 that could come in pretty handy.

1. Resource Management taken care of Try-Catch Construct Automatically! 

Previously in JDK 6, if we open a system resource like a File Stream, a network stream or simply a Buffered Reader within a Try block, you would probably use a finally block to make sure that the opened resources are closed before exiting the Try-Catch Constructs. For example..

 
    FileInputStream input_file;
 try
    {
        input_file = new FileInputStream("MyFile.txt");
        //do stuff with the stream

    }
    catch (FileNotFoundException e)
    {
            e.printStackTrace();
    }
    finally
    {
        if (input_file != null)
        {
            input_file.close();
        }
    }

But in Java 7, the try block has had a minor modification that allows you to open resources in the try block and do stuff with in winthin the try block and dont worry about closing them. As soon as control exits the Try block, all open resources will be closed automatically! So need to do the clean up work! Java will do it for you. The following code is a Java 7 remake of above code….

    try(FileInputStream input_file = new FileInputStream("MyFile.txt"))
    {
        //do stuff with the stream 
    }
    catch (FileNotFoundException e)
    {
            e.printStackTrace();
    }

2. Multi-Catch blocks

Tired of writing long sequences of Catch blocks to catch different types of Exceptions? Worry no more! The Multi Catch Statement is here! Take a look. This is what you would probably do in Java 6 and before.

    try
    {
        FileInputStream input_file = new FileInputStream("MyFile.txt");
        //code that may throw exceptions
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    catch(FileNotFoundException e)
    {
        e.printStackTrace();
    }
    catch(InterruptedIOException e)
    {
        e.printStackTrace();
    }

Now you can do it the easy way using the modification made to the catch block!

    try
    {
        FileInputStream input_file = new FileInputStream("MyFile.txt");
        //code that may throw exceptions
    }
    catch(FileNotFoundException | InterruptedIOException | IOException e)
    {
        e.printStackTrace();
    }

3. Strings in Switch Statements

Traditionally C++ and Java dont support using Strings as parameter in Switch Constructs. But Java 7 allows you to use String objects in switch cases. So no need to write long If-else constructs with .equals() method for Strings. Look at the following code…

String name=scannerObj.nextLine();
    switch(name)
    {
        case "Steve":
            System.out.println("Hello Steve!");
            break;
        case "Vishnu":
            System.out.println("Hello Vishnu!");
            break;
        case default:
            System.out.println("GTHO!");
            break;
    }

4.  Simpler Collections creating syntax:

Ever created a Map collection with a sub collection class in it? If so you might have been annoyed with having to type the “Type” specification on the right side of the statement as well. But now Java 7 requires you to specify the Type only on the left side and you can safely place an empty “<>” on the left side!

Map<String, List<String>> myMap = new HashMap<String, List<String>>(); //Before JAVA 7

Map<String, List<String>> myMap = new HashMap<>(); //After JAVA 7

5. Binary Literals:

Java 6 and before allow you to created only integer, Octal  and Hex literals. But you might want to store binary data in an integer variable. Java 7 helps you with that. It allows you to create a binary literal using 0b prefix.

int binary=0b0010;  //represents '2'

6. Ruby style Underscores in numbers:

Some languages like Ruby allows programmers to separate long numbers into more readable form using underscores (like using commas to separate digits – 1,000). This is pretty useful when dealing with large numbers.

int salary=25_00_000;  //equivalent to '2500000'

You can download Java 7 from here (http://www.oracle.com/technetwork/java/javase/downloads/index.html). You can select the appropriate version for you, SE or EE or ME etc. And downloading the JDK is enough. It contains the JRE as well. No need to download the JRE if you have downloaded the Java JDK. And dont forget to change your path variable after installing Java 7!

So what do you think about the latest JDK? Do you find the features useful? Please tell me what you think by leaving your comments here….!

Regards,

Steve Robinson

13 thoughts on “Java 7 is Here!

  1. Pingback: We’ve hit 5000! « Footy 'n' Tech

  2. First, thank you very much for this post!!! Please keep going more on Java…

    I have a question on Multi-catch feature

    before 7

    try
    {
    FileInputStream input_file = new FileInputStream(“MyFile.txt”);
    //code that may throw exceptions
    }
    catch(IOException e)
    {
    e.printStackTrace();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }

    do we need to follow the same rule of having higher version of exception classes at the bottom.

    catch(Exception | IOException e)
    {
    e.printStackTrace();
    }

    Will Java 7 take care if we have Exception class before IOException. Does ordering is important in Java 7 ???

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s