New in Dolphin - Java 7


With Dolphin java introduced cool new features like:

1. Strings in “switch” block

     switch(s) {
          case “Apple”:
          // do something;

          case “Orange”:
          // do something;


          default :
     }



     2. <> (Diamond) operator – Type inference for generic instance creation
E.g List > list = new ArrayList>();
vis-à-vis
List > list = new ArrayList <> ();
Empty diamond braces is required and it's not a typo :).



     3. Single Catch for multiple Exceptions with "|" operator
          try {
          // Reflective operations calling Class.forName,
          // Class.newInstance, Class.getMethod, Method.invoke, etc.
     } catch (final ClassNotFoundException |      InstantiationException | NoSuchMethodException |      InvocationTargetException e) {
          log(e)
          throw e;
     }


     4. Also in Java 7, this will work
     public void rethrowException(String exceptionName) throws FirstException, SecondException {
          try { // ...
          } catch (Exception e) {
          throw e;
     }
}



     5. Try-with-resources statement – basic objective is to facilitate automatic resource management, simplify coding and highlight pertinent exceptions e.g.
     try (InputStream in = new FileInputStream(src);
     OutputStream out = new FileOutputStream(dest);) {
          byte buf = new byte[8192];
          int n;
          while ((n = in.read(buf)) >= 0)
               out.write(buf, 0, n);
          }

Comments

  1. Oh these are nice - certainly will take care of much of the boiler plate code that one has to write for many of these exceedingly common situations! Thanks - was useful.

    ReplyDelete
  2. Thanks Sachin, I agree try block looks much better now...

    ReplyDelete
  3. Nice to see this blog Subodh, My name is also Subodh Gupta :D

    ReplyDelete

Post a Comment

Popular posts from this blog

java.net.SocketException: Connection reset

Hide empty last column from Table/Tree Viewer

Spring: implements interface not working in @Controller