16 May 2012

Java 7 (Dolphin) new features (edit)

Now that Java 7 will soon become the default Java JRE version for download, let's have a look at some new features:

String switch

You can use a String as a selector for a switch/case statement now

Enhanced try/catch

  • multicatch: catch multiple exceptions in one catch statement
    try {  
     ... 
    
    }catch(IOException | FileNotFoundException ex){…}
  • resources implementing java.lang.AutoCloseable resources (most resources in the JDK) can be automatically closed in a try-with-resources. You do not need a finally clause for this anymore.
    • Java 6 code
      try {                
        Scanner input = new Scanner (new File(“client.txt”));            
        …             
      } finally {             
        input.close();
      }
    • Java 7 code
      try(Scanner input = 
        new Scanner (new File(“client.txt”))){ 
        …
      }
    • You can also have multiple resources in a try
      try(Scanner input = 
        new Scanner (new File(“client.txt”)); 
        Formatter output = 
        new Formatter (new File(“adress.txt”)){ 
        …
      }

NIO.2 revamped file handling

is discussed in a separate blog entry.

Small syntax enhancements

  • Diamond operator
  • When the compiler can infer a generics type in a constructor, you can leave it empty
    List<Currency> currencies= new ArrayList<> ();
  • Literal enhancements
  • // binary numbers
    int localhost=0b1111111000000000000000000000001; 
    //underscores (for readability)
    double million=2_000_000.00; 

static java.util.Objects utilities

Examples:
  • null tolerant equals/hashCode/toString

  • hash (Object... values)

CSS styled javadoc 

With Java 7 Oracle is starting to modernise javadoc. In this release javadoc is restyled:












I'm not a big fan of the new look because the method names do not stand out enough for easy scanning the text for the method you need. Fortunatly the pages are styled using CSS. So I went to the topdirectory of the javadoc API and appended the contents of this file to stylesheet css, et voila:











The -stylesheetfile <path> option can be used to specify a stylesheet other than the default when using the javadoc command to generate the documentation.

Fork/Join

Fork/Join is an additional concurrent ExecutorService. It allows a pool of threads to concurrently work on a task, that can dynamically be split up into parallel subtasks. A busy thread is able to chew of a part of the task he’s doing and make it available for other threads.

JDK7

This is not really part of the Java language, but some tools in the JDK were upgraded.

JavaDB

JavaDB was updated to a more recent version of Apache Derby (10.8). Some noteworthy new features:
  • SQL Sequences
  • In memory databases (with disk based backup/restore)
  • SSL/TLS client/server communication
  • role based authorisations
  • stored procedures with elevated permissions

No comments:

Post a Comment