Thursday, February 24, 2011

Project Coin Examples with JDK 7 Developer Preview

Mark Reinhold announced availability of the first JDK 7 developer preview today. Reason enough for me to give some of the Project Coin features a shot. I won't get into details here, but instead just show some quick examples. Most of them are self-explanatory anyway.

Source code is also available via github.

Strings in switch

  1. final String str = "foo";  
  2.   
  3. switch (str) {  
  4.     case "foo":  
  5.         System.out.println("mooh");  
  6.         break;  
  7.     case "bar":  
  8.         System.out.println("miau");  
  9.         break;  
  10.     default:  
  11.         break;  
  12. }  

Binary integral literals and underscores in numeric literals

  1. final int large = 1_000_000;  
  2. System.out.println(large);  
  3.      
  4. final int binary = 0b1011;  
  5. System.out.println(binary);  

Multi-catch and more precise rethrow

  1. class ExA extends Exception {}  
  2. class ExB extends Exception {}  
  3.   
  4. public void testMultiCatch() {  
  5.   
  6.     final int a = 0;  
  7.           
  8.     try {  
  9.         if (a == 0) {  
  10.             throw new ExA();  
  11.         }  
  12.         if (a == 1) {  
  13.             throw new ExB();  
  14.         }  
  15.     } catch (ExA|ExB ex) {  
  16.         System.out.println(ex.getClass() +   
  17.              " was thrown and caught");  
  18.     }  
  19.   
  20. }  
  21.   
  22. public void testReThrow() throws ExB {  
  23.   
  24.     final int a = 0;  
  25.   
  26.     try {  
  27.         if (a == 0) {  
  28.             throw new ExA();  
  29.         }  
  30.         if (a == 1) {  
  31.             throw new ExB();  
  32.         }  
  33.     } catch (final ExA exa) {  
  34.         System.out.println("Exa was caught");  
  35.     } catch (final Exception ex) {  
  36.         System.out.println(ex.getClass() +   
  37.             " was thrown, caught and rethrown");  
  38.         throw ex;  
  39.     }  
  40.   
  41. }  

Improved type inference for generic instance creation (diamond)

  1. final List<String> list = new ArrayList<>();  
  2. list.add("Foo");  
  3.           
  4. System.out.println(list);  

try-with-resources statement

  1. try (final BufferedReader br = new BufferedReader(new FileReader("./TestAutomaticResourceManagement.java"))) {  
  2.   
  3.    String line;  
  4.    while ((line = br.readLine()) != null) {  
  5.        System.out.println(line);  
  6.    }  
  7.   
  8. catch (final IOException e) {  
  9. }  

Friday, January 28, 2011

Oracle 1-year review

It's been about a year now since Oracle aquired Sun. Here is a short review of what happened since then:
  • Oracle pulled the plug on the servers of the PostgreSQL build farm with no warning, see here.

  • Oracle kills OpenSolaris? OpenSolaris Board Quits En Masse, see here and here.

  • Prominent Sun employees leave the company, including James Gosling, Tim Bray, Simon Phipps, Tor Norbye, Kohsuke Kawaguchi, see here.

  • Oracle sues Google over use of Java-related patents in Android's Dalvik VM and SDK, see here.

  • OpenOffice.org declares independence from Oracle, becomes LibreOffice, see here.

  • Doug Lea leaves the JCP, see here.

  • Oracle nominates Hologic for JCP, see here.

  • Java SE 7 and SE 8 specs move forward amid protest, see here.

  • Tim Peierls resigned from JCP EC, see here.

  • Apache resigns from JCP EC, see here.

  • Hudson wants to move from Oracle. This story is still going on, see here and here.

  • Ruby on Rails support discontinued in NetBeans IDE, see here.