Java 7 New Features

As with every new Java version, Java 7 introduced some new features. The new features are definitely worth the change and as developers, we must be happy as we can be more efficient. There are many features introduced in Java 7 but we will look at some of them.

  1. Allows Strings to be used in ‘case’ switch statements.
  2. Try with resources
  3. Custom Autocloseable Class
  4. Using underscore characters in numeric values.
  5. Multi-catch Exceptions
  6. Improved Type reference for Generic Instance creation
  7. Binary literals

Lets look at these features one by one.

Strings In Switch Statements

In all computer languages that I know of, switch case statements can switch either on int or char type. If at all, we needed to switch based on Strings, then we had to figure out some workarounds using char data types or else switch to “if-else” logic to do that. And Java allowed ‘enum’ types also to be used in switch statement. But still we were not able to switch based on strings in switch case statements till Java 7.

Java 7 officially supports using Strings in switch case statements. This can definitely save developers some time instead of opting for workarounds. A good research topic would be trying to figure out the reasons Java took so long for Strings to be included in switch case statements!. An example is illustrated below: (this code will not compile in any version below Java 7)

public class TestSwit {

    public static void main(String[] args) {

 String java7_switch;
 System.out.println("Enter a test string");
 java7_switch = StdIn.readString();

 switch (java7_switch) {
  case "Java 4":
             System.out.println("Java 4");
      break;
  case "Java 2":
      System.out.println("Java 5");
      break;
  case "Java 6":
      System.out.println("Java 6");
      break;
  case "Java 7":
      System.out.println("Java 7 ...");
 }
    }

}

Try With Resources Statements

This is my favorite of all the new features in Java 7 and shortly, you will see the reasons for my bias towards this particular feature. Lets look at an example.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Java6TryCatch {

 public static void main(String[] args) {
       String filename = "test.txt";
       BufferedReader br = null;
       FileReader test = null;

       try {
     test = new FileReader(filename);
     br = new BufferedReader(test);
     String lines = "";

     while ((lines = br.readLine())!=null){
          System.out.println(lines);
     }
       } catch (IOException | SQLException e) {
   e.printStackTrace();
       } finally {
          try {
      br.close();
      test.close();
   } catch (IOException e) {
             e.printStackTrace();
   }
       }

 }

}

This program is pretty straightforward to understand. It reads from a file line by line and prints out the content. The class FileReader throws IOException and the BufferedReader class throws a checked IOException. Since these are checked exceptions, I am surrounding this piece of code in a try-catch block. And at last, I need to close the opened resources and so use br.close() in the finally block to close the file. This finally block will be called regardless of an exception occurs or not. And then again in the finally block, since br.close() throws a checked exception, that is also surrounded with try-catch block.

Think of a scenario where there are 10 checked exceptions that need to be handled. Either we can throw them to the parent class and catch them all under the parent java.io.Exception class. But applications needs sophisticated error messages to be displayed and so we need to handle each exception differently. So we will have 10 try-catch blocks, and one finally block to clean up all resources. Again, we may have several try-catch blocks inside finally block as it may throw some checked exceptions just like shown in our example.

Do you get an idea of the volume of code in catch block first and then almost similar exceptions being handled in finally block while cleaning up opened resources. Wouldn’t it be if Java made it simpler to code?. Yes. Java 7 does this exactly and it is called Try-with-Resources statement. This is how it works.

try(BufferedReader buff = new BufferedReader(
                               new FileReader(filename)))
{
  String lines = "";

  while ((lines = br.readLine())!=null) {
   System.out.println(lines);
  }
}

Almost 5-6 lines of code has been coded in just one line of code. This method of handling the cleanup of resources using only “try” statement is called Try-With-Resources statement. But then are we missing something? You may ask where is the br.close() method!. In our example, the FileReader and BufferedReader classes implement java.lang.AutoCloseable interface, which takes care of closing these resources. I will also provide an example of this AutoCloseable interface in this article.
Also, multiple try-with resources can be used by separating them with a semi-colon; eg.,

try(FileReader fRead = new FileReader(filename);
            BufferedReader buff = new BufferedReader(fRead)
{

     /* Other code here */
}

If you are wondering in which order these resources will be closed. Javadocs has an answer. It will be closed in the opposite order it appears in the Try-With-resources statement. So, in our case first buff.close() will be called and then fRead.close() will be called. Also, what about the exceptions handling. Though the fRead.close() and buff.close() methods implements AutoCloseable interface, exceptions in the try block exceptions take preference over the close() exceptions. In other words, if exceptions occur in try-with-resources statement and close() method, only the try-with-resource exception will be passed back to the stack and the close() methods exception will be suppressed. Also if needed, there are methods to get this suppressed exception also. So the try-with-resources exception take precedence over exceptions in AutoCloseable interface.

Custom AutoCloseable Class

I will provide an example, which extends this AutoCloseable interface so that we can use it if required in our own classes.

java.lang.AutoCloseable interface:

public interface AutoCloseable {
 public void close() throws Exception;
}

A test class extending the AutoCloseable interface. Any class implementing AutoCloseable interface has to implement only the close () method.

class TryClose implements AutoCloseable {

 @Override
 public void close() throw Exception {
  System.out.println(" Custom close method …
                                         close resources ");
 }
}

Underscore Character In Numeric Values

This was introduced to improve the code readability. Underscores can be used wherever we use numeric values. Assume that you want to declare a number i.e., say 1 million, prior to Java 7, declaration would be something like this:

int million = 1000000;

This may be hard to read, as the user may have to manually count the zeros and then figure out that if it is 1 million/10 million. Wouldn’t it be nice if we are allowed to declare as 1,000,000 clearly separating 1 million from 10 million using commas just as we do in Maths. Wouldn’t it improve code readability?

Exactly. This is what Java 7 address. Instead of comma, we are allowed to use underscore character (_) between numeric values.

int million = 1_000_000;
double amount = 60.50_40_30;

Though there are certain rules that must be kept in mind, that is a _ can’t begin a numeral, a _ can’t precede/succeed a decimal point, it helps developers make the code readable.

int million = _100; // invalid
double amount = 60._50; // invalid

Multi-Catch Exceptions

Lets look at the next feature – multi –catch exceptions. Before Java 7, we needed to have catch block for each of the different exceptions that needed to be handled.

try
{
    new FileReader(filename);
    br = new BufferedReader(test);
    String lines = "";
    while ((lines = br.readLine())!=null){
       System.out.println(lines);
    }
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 } catch (IOException e){
  e.printStackTrace();
 }

From Java 7, we can handle multiple exceptions as part of the same catch block. It basically does the OR condition check and we can have as many exceptions as need and handle it the same way.

try
{
     new FileReader(filename);
     br = new BufferedReader(test);
     String lines = "";
     while ((lines = br.readLine())!=null){
       System.out.println(lines);
     }
 } catch (FileNotFoundException | IOException e) {
  e.printStackTrace();
 }

Improved Type reference For Generic Instance

This feature saves some coding/typing time. Before Java 7, we had to type this in order to compile,

ArrayList<string> list2 = new ArrayList<String>();

Now, from Java 7, we can safely ignore the right hand type, as Java will automatically infer the type from the left hand side. So we could write as below:

ArrayList<String> list2 = new ArrayList<>();

One thing to note is that we mustn’t ignore the <> operator on the right, otherwise we will get a compiler error. This operator is called the Diamond operator.

Binary Literals

From Java 7, (byte,short,int,long) can also be expressed in binary number system. We just need to add a prefix “0b” or “OB” before the number.

int binInt1 = 0b100;
Long binLong = 0B100000000000L;

That’s it for this article on some of the new features introduced in Java7. Hope you enjoyed reading. Thanks for reading this article on Java 7 new features list.

This article is originally published at Java tutorials – Lets jump into the ocean, re-posted here with authors permission and as part of the JBC program.

Comments

comments

About Rengasudharsan Srinivasan

Renga is currently working as a software engineer. He has experience developing web applications using spring, struts and Hibernate framework. He is passionate about learning new technologies and has recently started writing a blog http://renga86.blogspot.com mainly focussing on Java.

Comments

  1. // below slipeets works fine
    try(BufferedInputStream bis=new BufferedInputStream(new FileInputStream(new File(“f”)))){

    }catch (Exception e) {
    // TODO: handle exception
    }

    // If I write separate then file instance is not auto closable, Could you explain how it works internally.
    try( File f=new File(“”);
    FileInputStream fis=new FileInputStream(f);
    BufferedInputStream bis=new BufferedInputStream(fis) ){

    }catch (Exception e) {
    // TODO: handle exception
    }

    • Thanks Siddu. Ya you are right … If the parent class Exception exists, then we can’t include the child class Exception in the multi try-catch block … i wanted to have some other exception but in my attempt to give the same example as in Java 6, I didn’t notice this … will change it …

    • siddu, AutoCloseable is only applicable for resources which needs to be closed after they are opened for IO. In case of File, we are not opening/closing a resource but just checking if the file exists in that path or not. Only when using FileReader or FileInputStream, you start reading that file line by line/character by character and stuff like that … That is why, if u check Java Docs java.io.File doesn’t implement AutoCloseable interface but rather FileReader and BufferedReader does … so this will work because try-with-resources should contain only classes implementing AutoCloseable …

      try( FileInputStream fIn = new FileInputStream(new File(“f”));
      BufferedInputStream bIn = new BufferedInputStream(fIn)) {
      } catch (Exception e) {
      e.printStackTrace();
      }
      to understand better, check all the API under File and u will find only APIs which check for file exists, path valid and not for reading from the file … hope this clarifies your question …

  2. try

    {

    new FileReader(filename);

    br = new BufferedReader(test);

    String lines = “”;

    while ((lines = br.readLine()) != null) {

    System.out.println(lines);

    }

    } catch (FileNotFoundException | IOException e) { // This snippet is wrong. Since FileNFE is suclass of IOE. So it wont allows subtypes here. We can only assign different type. IOE is the one handles FNFE as well as IOE so thats why it wont support both.

    e.printStackTrace();
    }
    please re correct if I am wrong

    • Thanks Siddu. Ya you are right … If the parent class Exception exists, then we can’t include the child class Exception in the multi try-catch block … i wanted to have some other exception but in my attempt to give the same example as in Java 6, I didn’t notice this … will change it …

  3. ArrayList list5 = new ArrayList(); // This takes only Double
    ArrayList list5 = new ArrayList(); // This takes only stirng
    // The above snippets makes type safety why not below snippets.
    ArrayList list5 = new ArrayList(); // This is not generics declaration, This accept anything.
    list5.add(4354);
    list5.add(“df”);

    • if you are referring to Improved Type reference For Generic Instance section, ArrayList string = new ArrayList(), that is a typo and is has been already changed. probably might take some time to get reflected.

      this is what i meant:
      ArrayList listStr = new ArrayList(); /// before java 7
      After Java 7,
      ArrayList listStr = new ArrayList();

      i didn’t quite get your question, can you please make it a little clear…

    • Sorry i posted wrong snippets.
      Below snippets
      ArrayList list2 = new ArrayList(); // makes type safety..
      ArrayList list3 = new ArrayList(); // makes type safety..
      ArrayList list4 = new ArrayList(); // makes type safety..
      Why not below snippet wont make type safety. any technical reason.
      ArrayList list5 = new ArrayList(); /// the only change is i declared right hand side

      • still all your lines of code look the same … sorry, i don’t see any difference in ur code snippet… but you are saying it makes “type safety” which might be incorrect. suppose you have ArrayList list2 = new ArrayList(); this declaration doesn’t stop you from loading all types of objects : strings, double, Integer … suppose you want to get all the elements, how will you retrieve this heterogenous data? its all mixed data … so always use parameterized like ArrayList lsit2 = new ArrayList() so that user/anyone who looks at the code knows this list can store only string items …

        • ArrayList list2 = new ArrayList();

          • Its Ok, I think some issues with submission of data. It is not posting properly. I posted generic declaration. but its not showing. anyway thanks for reply.

  4. Could you try to post Servlets 3.0 features with examples?
    I have one more questions.
    List list=new ArrayList vs ArrayList list=new ArrayList() // Which is the best.. Could you tell me the scenarios.

  5. Why constructor name is same as class name? any technical reason.
    why there is only one public class per source code in java
    why hashmap allows null key and values & not by hashtable?
    How to use user defined objects as key for hashmap. Could you give one example.
    Why key objects should be immutable? Whats the benefits?

Speak Your Mind

*