Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java 7.0 change summary A view of possible small language changes CHANGE BULLETIN BOARD Better Integer Literal Improved type inference Enum comparison String switch Management Chained invocations & Extension methods Improved catch clauses Array notation for Map, List Self type(this) Automatic Resource New I/O 2 (NIO2) Libraries Fork Join Framework Miscellaneous Things •What is better Integer Literal ? Before JDK1.7 Binary literals Int mask=0b101010101010; Proposed in JDK1.7 With underscores for clarity int mask =0b1010_1010_1010 ; long big =9_234_345_087_780L; •Improved type inference? Proposed in Jdk1.7 Constructors Before JDK1.7 Constructors Map<String, List<String>> anagrams = new HashMap<String, List<String>>(); Map<String, List<String>> anagrams = new HashMap<>(); •Improved type inference(cont) Before JDK1.7 Argument Positions public <E> Set<E> emptySet() { … } void timeWaitsFor(Set<Man> people) { …} // * Won't compile! timeWaitsFor(Collections.emptySet()); Proposed in Jdk1.7 Argument Positions public <E> Set<E> emptySet() { … } void timeWaitsFor(Set<Man> people) { …} // OK timeWaitsFor(Collections.<Man>empt ySet()); •What is new Enum Comparison? Before JDK1.7 enum Size { SMALL, MEDIUM, LARGE } enum Size { SMALL, MEDIUM, LARGE } if (mySize.compareTo(yourSize) >= 0) System.out.println(“ You can wear my pants.”); Proposed in Jdk1.7 if (mySize > yourSize) System.out.println(“Y ou can wear my pants.”); What is new Switch Case? Before JDK1.7 static boolean booleanFromString(String s) { if (s.equals("true")) { return true; } else if (s.equals("false")) { return false; } else { throw new IllegalArgumentException(s); } } static boolean booleanFromString(String s) { switch(s) { case "true": return true; case "false": return false; default: throw new IllegalArgumentException( s); } } Proposed in Jdk1.7 Chained invocations Before JDK1.7 (chained) class Builder { void setSomething(Something x) {…} void setOther(Other x) { … } Thing result() { … } } Builder builder = new Builder(); builder.setSomething(something); builder.setOther(other); Thing thing = builder.result(); Proposed in Jdk1.7 class Builder { void setSomething(Something x) {…} void setOther(Other x) { … } Thing result() { … } } Thing thing = new Builder() .setSomething(something) .setOther(other) .result(); Extension Methods Before JDK1.7 import java.util.Collections; List<String> list = …; Collections.sort(list); Proposed in Jdk1.7 import static java.util.Collections.sort; List<String> list = …; list.sort(); •Improved catch clauses : Catching multiple types Before JDK1.7 try { return klass.newInstance(); }catch (InstantiationException e) { throw new AssertionError(e); }catch (IllegalAccessException e) { throw new AssertionError(e); } Proposed in Jdk1.7 try { return klass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new AssertionError(e); } •Improved catch clauses : rethrown exceptions Before JDK1.7 try { // Throws several types doable.doit(); } catch (Throwable ex) { logger.log(ex); throw ex; // Error: Throwable not declared } try { // Throws several types doable.doit(); } catch (final Throwable ex) { logger.log(ex); throw ex; // OK: Throws the same several types } Proposed in Jdk1.7 •Array notation for Map, List change Before JDK1.7 void swap(List<String> list, int i, int j) { String s1 = list.get(i); list.set(i,list.get(j)); list.set(j, s1); } Proposed in Jdk1.7 void swap(List<String> list, int i, int j) { String s1 = list[i]; list[i] = list[j]; list[j] = s1; } •Array notation for Map, List change(cont) Before JDK1.7 Map<Input,Output> cache = …; Output cachedComputation(Input in) { Output out = cache.get(in); if (out == null) { out = computation(input); cache.put(in, out); } return out; } Proposed in Jdk1.7 Map<Input,Output> cache = …; Output cachedComputation(Input in) { Output out = cache[in]; if (out == null) { out = computation(input); cache[in] = out; } return out; } •What is Self type(this)? Before JDK1.7 class Buffer { Buffer flip() { … } Buffer position(int newPos) { … } Buffer limit(int newLimit) { … } } class ByteBuffer extends Buffer { ByteBuffer put(byte data) { … } } public static void main(String args) { Proposed in Jdk1.7 class Buffer { this flip() { … } this position(int newPos) { … } this limit(int newLimit) { … } } class ByteBuffer extends Buffer { this put(byte data) { … } } ByteBuffer buf = ...; public static void main(String args) { buf.flip().position(12); // OK ByteBuffer buf = ...; buf.flip().put(12); // Error } buf.flip().position(12); // OK buf.flip().put(12); // is fine now} •Why Automatic Resource Management? Before JDK1.7 InputStream in = new FileInputStream(src); try { OutputStream out = new FileOutputStream(dest); try { byte[] buf = new byte[8192]; int n; while (n = in.read(buf)) >= 0) out.write(buf, 0, n); } finally { out.close(); }} finally { in.close(); } •Why Automatic Resource Management?(cont) Proposed JDK1.7 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); } New superinterface java.lang.AutoCloseable All AutoCloseable and by extension java.io.Closeable types useable with try-withresources anything with a void close() method is a candidate JDBC 4.1 retrefitted as AutoCloseable too What is New I/O 2 (NIO2) Libraries? (JSR 203) Original Java I/O APIs presented challenges for developers Not designed to be extensible Many methods do not throw exceptions as expected rename() method works inconsistently Developers want greater access to file metadata Java NIO2 solves these problems Features of NIO2 (cont) Path is a replacement for File (Biggest impact on developers) Better directory support (list() method can stream via iterator) Entries can be filtered using regular expressions in API Symbolic link support java.nio.file.Filesystem interface to a filesystem (FAT, ZFS, Zip archive, network, etc) java.nio.file.attribute package Access to file metadata Features of NIO2 (cont) Path Class introduction Equivalent of java.io.File in the new API Immutable Have methods to access and manipulate Path Few ways to create a Path - From Paths and FileSystem //Make a reference to the path Path home = Paths.get(“/home/fred”); //Resolve tmp from /home/fred -> /home/fred/tmp Path tmpPath = home.resolve(“tmp”); //Create a relative path from tmp -> .. Path relativePath = tmpPath.relativize(home) File file = relativePath.toFile(); Introuction Fork Join Framework Goal is to take advantage of multiple processor Designed for task that can be broken down into smaller pieces Eg. Fibonacci number fib(10) = fib(9) + fib(8) Typical algorithm that uses fork join if I can manage the task perform the task else fork task into x number of smaller/similar task join the results Introuction Fork Join Framework (cont) Key Classes ForkJoinPool Executor service for running ForkJoinTask ForkJoinTask The base class for forkjoin task RecursiveAction A subclass of ForkJoinTask A recursive resultless task Implements compute() abstract method to perform calculation RecursiveTask Similar to RecursiveAction but returns a result Introuction Fork Join Framework (cont) ForkJoin Example – Fibonacci public class Fibonacci extends RecursiveTask<Integer> { private final int number; public Fibonacci(int n) { number = n; } @Override protected Integer compute() { switch (number) { case 0: return (0); case 1: return (1); default: Fibonacci f1 = new Fibonacci(number – 1); Fibonacci f2 = new Fibonacci(number – 2); f1.fork(); f2.fork(); return (f1.join() + f2.join()); } } } Introuction Fork Join Framework (cont) ForkJoin Example – Fibonacci ForkJoinPool pool = new ForkJoinPool(); Fibonacci r = new Fibonacci(10); pool.submit(r); while (!r.isDone()) { //Do some work ... } System.out.println("Result of fib(10) = "+ r.get()); •Miscellaneous Things InvokeDynamic Bytecode Security Eliptic curve cryptography TLS 1.2 JAXP 1.4.4 JAX-WS 2.2 JAXB 2.2 ClassLoader architecture changes close() for URLClassLoader Javadoc support for CSS