Java 7 is here! – Some Ease for Java Programmers


Java Development Kit 1.7 is released with some new ease to Java programmer. I had posted about Java NIO.2 filesystem in my previous post check it out here this stills holds true and is released. In this release 7 there are as expected changes in the Garbage Collector and JVM performance enhancements and many other changes. Well two of the features that i the most are:

1. inclusion of strings in switch statement, as a result now there is no need to add a large number of if-then-elseif statements anymore. Just add a switch. EASY!

2. try-with-resources statement: I like this feature the most as this will help in some manner to release the resources right after the try-catch block is executed. So no headache to add more lines of code in finally block for closing of resources. Following is quite a useful example from oracle website:

  public static void viewTable(Connection con) throws SQLException {

    String query = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES";

    try (Statement stmt = con.createStatement()) {

      ResultSet rs = stmt.executeQuery(query);

      while (rs.next()) {
        String coffeeName = rs.getString("COF_NAME");
        int supplierID = rs.getInt("SUP_ID");
        float price = rs.getFloat("PRICE");
        int sales = rs.getInt("SALES");
        int total = rs.getInt("TOTAL");
        System.out.println(coffeeName + ", " + supplierID + ", " + price +
                           ", " + sales + ", " + total);
      }

    } catch (SQLException e) {
      JDBCTutorialUtilities.printSQLException(e);
    }
  }

As you see above the stmt object will be closed right after the try-catch block is executed, and hence there is no requirement of writing a finally block with stmt.close() statement.

A more elaborated description on this feature is available on the oracle website here.

Further Reading:

http://download.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html

http://java.dzone.com/articles/java-7-new-try-resources

 

 

 

 

About Dominic

J for JAVA more about me : http://about.me/dominicdsouza
This entry was posted in Tech News. Bookmark the permalink.

Leave a comment