1. Language Changes (Project Coin) Try-with-Resources (AutoCloseable) Automatically closes resources implementing AutoCloseable .
int million = 1_000_000; long creditCard = 1234_5678_9012_3456L; Binary Literals int mask = 0b1010_0101; // prefix 0b or 0B 2. New File I/O (NIO.2) – java.nio.file Replaced legacy File class. java 7
// br automatically closed try // some code catch (SQLException | IOException e) // single block logger.log(e); throw new MyAppException(e); New File I/O (NIO
// Before Java 7 BufferedReader br = null; try br = new BufferedReader(new FileReader("file.txt")); br.readLine(); catch (IOException e) e.printStackTrace(); finally { if (br != null) try br.close(); catch (IOException e) {} } // Java 7 try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) br.readLine(); catch (IOException e) e.printStackTrace(); throw new MyAppException(e)
WatchService watcher = FileSystems.getDefault().newWatchService(); Path dir = Paths.get("/tmp"); dir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE); WatchKey key = watcher.take(); for (WatchEvent<?> event : key.pollEvents()) System.out.println("Event: " + event.kind() + " on " + event.context());