Wednesday, November 16, 11
Total Page:16
File Type:pdf, Size:1020Kb
Wednesday, November 16, 11 <Insert Picture Here> Top 10 things to know about Java SE 7 Danny Coward Principal Engineer Wednesday, November 16, 11 The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 3 Wednesday, November 16, 11 Java SE 7 Top 10 1. Project Coin 4 Wednesday, November 16, 11 coin, n. A piece of small change coin, v. To create new language 5 Wednesday, November 16, 11 Evolving the Language From “Evolving the Java Language” - JavaOne 2005 • Java language principles – Reading is more important than writing – Code should be a joy to read – The language should not hide what is happening – Code should do what it seems to do – Simplicity matters – Every “good” feature adds more “bad” weight – Sometimes it is best to leave things out • One language: with the same meaning everywhere • No dialects • We will evolve the Java language • But cautiously, with a long term view • “first do no harm” 6 Wednesday, November 16, 11 Better Integer Literals • Integer literals int mask = 0b101010101010; aShort = (short)0b1010000101000101; long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L; • With underscores for clarity int mask = 0b1010_1010_1010; long big = 9_223_783_036_967_937L; 7 Wednesday, November 16, 11 :) Literals HAPPY_FACE = { (short)0b0000011111100000; (short)0b0000100000010000; (short)0b0001000000001000; (short)0b0010000000000100; (short)0b0100000000000010; (short)0b1000011001100001; (short)0b1000011001100001; (short)0b1000000000000001; (short)0b1000000000000001; (short)0b1001000000001001; (short)0b1000100000010001; (short)0b0100011111100010; (short)0b0010000000000100; (short)0b0001000000001000; (short)0b0000100000010000; (short)0b0000011111100000; } 8 Wednesday, November 16, 11 Recognize any of these numbers ? 314159265 311385876 3315643000000 299792458 53500000000 9 Wednesday, November 16, 11 Recognize any of these numbers ? 3_14159265 311_385_876 3_315_643_000_000 299_792_458 53_500_000_000 9 Wednesday, November 16, 11 Recognize any of these numbers ? 3_14159265 π 311_385_876 3_315_643_000_000 299_792_458 53_500_000_000 9 Wednesday, November 16, 11 Recognize any of these numbers ? 3_14159265 π 311_385_876 est. US population 3_315_643_000_000 299_792_458 53_500_000_000 9 Wednesday, November 16, 11 Recognize any of these numbers ? 3_14159265 π 311_385_876 est. US population 3_315_643_000_000 German GDP 2010 299_792_458 53_500_000_000 9 Wednesday, November 16, 11 Recognize any of these numbers ? 3_14159265 π 311_385_876 est. US population 3_315_643_000_000 German GDP 2010 299_792_458 c m/s 53_500_000_000 9 Wednesday, November 16, 11 Recognize any of these numbers ? 3_14159265 π 311_385_876 est. US population 3_315_643_000_000 German GDP 2010 299_792_458 c m/s 53_500_000_000 Carlos Slim net worth 9 Wednesday, November 16, 11 Strings in Switch statements • Today case label includes integer constants and enum constants • Strings are constants too (immutable) 10 Wednesday, November 16, 11 int monthNameToDays(String s, int year) { if (s.equals(“April”) || s.equals(“June”) || s.equals(“September”) || s.equals(“November”)) return 30; if (s.equals(“January”) || s.equals(“March”) || s.equals(“May”) || s.equals(“July”) || s.equals(“August”) || s.equals(“December”)) return 31; if (s.equals()) ... else ... } 11 Wednesday, November 16, 11 int monthNameToDays(String s, int year) { switch(s) { case “April”: case “June”: case “September”: case “November”: return 30; case “January”: case “March”: case “May”: case “July”: case “August”: case “December”: return 31; case “February”: ... default ... } 11 Wednesday, November 16, 11 Diamond Operator Map<Bar, Foo> map1 = new HashMap<Bar, Foo>(); Map<Bar, List<Foo>> map2 = new HashMap<Bar, List<Foo>>(); Map<Bar, List<List<Foo>>> map3 = new HashMap<Bar, List<List<Foo>>>(); Wednesday, November 16, 11 Diamond Operator Map<Bar, Foo> map1 = new HashMap<>(); Map<Bar, List<Foo>> map2 = new HashMap<>(); Map<Bar, List<List<Foo>>> map3 = new HashMap<>(); Wednesday, November 16, 11 Copying a File 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); in.close(); out.close(); 13 Wednesday, November 16, 11 Copying a File InputStream in = new FileInputStream(src); 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 { in.close(); out.close(); } 14 Wednesday, November 16, 11 Copying a File 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(); } 15 Wednesday, November 16, 11 Copying a File 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 { Exception thrown from in.close(); potentially three places. } Details of first two could be lost 16 Wednesday, November 16, 11 try-with-resources syntax 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); } 17 Wednesday, November 16, 11 try-with-resources syntax try ResourceSpecification Block Catchesopt Finallyopt 18 Wednesday, November 16, 11 try-with-resources syntax • Compiler desugars try-with-resources into nested try- finally blocks with variables to track exception state • ‘Lost’ exceptions are recorded for posterity using a new facility of Throwable - addSuppressedException • API support in JDK 7 • anything with a void close() method is a candidate for use with this syntax • i.e. anything implementing java.io.Closeable or its new superinterface java.lang.AutoCloseable • JDBC 4.1 retrofitted as AutoCloseable too 19 Wednesday, November 16, 11 More Informative Backtraces java.io.IOException at Suppress.write(Suppress.java:19) at Suppress.main(Suppress.java:8) Suppressed: java.io.IOException at Suppress.close(Suppress.java:24) at Suppress.main(Suppress.java:9) Suppressed: java.io.IOException at Suppress.close(Suppress.java:24) at Suppress.main(Suppress.java:9) 20 Wednesday, November 16, 11 Multi-catch with More Precise Rethrow try { // Reflective operations calling Class.formName, // Class.newOnstance, Class.getMethod, Method.invoke, etc. } catch (ClassNotFoundException cnfe) { log(cnfe) throw cnfe; } catch (InstantiationException ie) { log(ie); throw ie; } catch (NoSuchMethodException nsme) { log(nsme); throw nsme; } catch (InvocationTargetException ite) { log(ite); throw ite; } 21 Wednesday, November 16, 11 Tempting... try { // Reflective operations calling Class.formName, // Class.newInstance, Class.getMethod, Method.invoke, etc. } catch (Exception e) { log(e) throw e; } 22 Wednesday, November 16, 11 Exception by-catch try { // Reflective operations calling Class.formName, // Class.newOnstance, Class.getMethod, Method.invoke, etc. } catch (Exception e) { log(e) throw e; } Catches both checked and unchecked exceptions 23 Wednesday, November 16, 11 Exception by-catch try { // Reflective operations calling Class.formName, // Class.newOnstance, Class.getMethod, Method.invoke, etc. } catch (RuntimeException re) { ... } catch (Exception e) { log(e) throw e; } Better 24 Wednesday, November 16, 11 Multi-catch - best try { // Reflective operations calling Class.formName, // Class.newOnstance, Class.getMethod, Method.invoke, etc. } catch (ClassNotFoundException | InstantiationException | NoSuchMethodException | InvocationTargetException e) { log(e) throw e; } 25 Wednesday, November 16, 11 Java SE 7 Top 10 2. NIO2 26 Wednesday, November 16, 11 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 27 Wednesday, November 16, 11 Java NIO2 API Features • 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 28 Wednesday, November 16, 11 The Path Class • 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(); 29 Wednesday, November 16, 11 File Operation – Copy, Move • File copy is really easy – With fine grain control Path src = Paths.get(“/home/fred/readme.txt”);