Successfully reported this slideshow.

From Java 6 to Java 7 reference

3,763 views

Published on

Java 6 -> Java 7 reference: NIO, Exception, Join Fork framework, Syntax

From Java 6 to Java 7 reference

  1. 1. Just to drink a cup of coffee refence guide: from java 6 to java 7 (SE) Syntax Compiler infers the generic type parameters Improving variables declarations Numbers litterals Improving numebr reading (really?) String switch Exception Catch more exceptions Avoiding code redundancy Try with resources Avoiding “if/try/catch/finally nightmare” Multithread Fork and Join Framework Parallel computing NIO deleteIfExists, … ...and a lot of stuff File Watcher Java Typed java.lang.invoke Not typed language are easy
  2. 2. Compiler infer the generic type parameters Java 6 bject> objects = new LinkedList<MyObject>(); Java 7 List<MyObject> objects = new Lin
  3. 3. Numbers Java 6 int i = 1000; int j =1000000 Java 7 int i = 1_000; int j = 1_000_000; Wrong syntax int x = _1_000_000;
  4. 4. String switch Java 6 String a,b,c; if (a.equals(b)) { } else if (a.equals(c)) { } Java 7 String a,b,c; switch (a) { b: break; c: break; }
  5. 5. Catch more exceptions Java 6 try { ... } catch(NumberFormatException e) { logError(e); } catch(NullPointerException e) { logError(e); } Java 7 try { ... } catch(NumberFormatException | logError(e); }
  6. 6. Try with resources (implements Closeable or AutoCloseable) Java 6 FileReader r = null; try{ r = new FileReader(file); BufferedReader reader = new BufferedReader(r); String line = reader.readLine(); while (line != null) { lines.add(line); line = reader.readLine(); } } catch(IOException e) { logError(e); } finally { if (r!=null) r.close(); } Java 7 try(BufferedReader reader = new BufferedReader( String line = reader.readLine(); while (line != null) { lines.add(line); line = reader.readLine(); } }
  7. 7. Join Fork Framework more powerfull than the code described here [http://www.oracle.com/technetwork/articles/java/fork-join-422606.ht Java 6 FutureTask<String> myFutureAction = new FutureTask<String>( new Callable<String>() { public MyObject call() { Return ...; } }); ExecutorService executor = Executors.newFixedThreadPool(1); FutureTask<Object> futureOne = new FutureTask<Object>(myFutureAction); executor.execute(future); …//sleep executor.shutdown(); Java 7 public class ForkAction extends RecursiveAct ParamObject from = null; ForkAction(ParamObject from, ParamObject to) this.from=from; ... } @Override public MyObject compute() { ... } } ... ForkJoinPool forkJoinPool = new ForkJoinPool forkJoinPool.invoke(new ForkAction(from, to)
  8. 8. NIO deleteIfExists, ... Java 6 try { Files.delete(path); } catch(NoSuchFileException e){} Java 7 Files.deleteIfExists(path); and a lot of stuff Files.createSymbolicLink(...) Files.copy(...) Files.move(...)
  9. 9. NIO WatcherService Java 6 while (true) { File myDir = … pollForChanges(myDir); } public void pollForChanges(File myDir) { //list dir //use apache common io } Java 7 Path pathToWatch = Paths.get("/tmp"); PathToWatch.register(FileSystems.getDefault( while (true) { WatchKey key = watchService.take(); }
  10. 10. NIO WatcherService Java 6 while (true) { File myDir = … pollForChanges(myDir); } public void pollForChanges(File myDir) { //list dir //use apache common io } Java 7 Path pathToWatch = Paths.get("/tmp"); PathToWatch.register(FileSystems.getDefault( while (true) { WatchKey key = watchService.take(); }
  11. 11. java.lang.invoke Java 6 class MyClass { public void myMethod(int i, String j) { ... } } ... Class<?>[] argTypes = new Class[] { int.class, String.class }; meth = MyClass.class. getDeclaredMethod("myMethod", argTypes); meth.invoke(new MyClass(), 2, "EFG"); Java 7 class MyClass { public void myMethod(int i, String j) { ... } } ... MethodHandle mh; MethodType desc = MethodType.methodType(v mh = MethodHandles.lookup().findVirtual(M mh.invokeExact(new MyClass(), 1, "ABCDE")

×