Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Java 9 - what else is new?

Java 9 - what else is new?

Most Java developers have heard about Jigsaw - the new Java module system. But what else does Java 9 provide when it comes to new features and changes?

Jan Fredrik Wedén

December 16, 2017
Tweet

More Decks by Jan Fredrik Wedén

Other Decks in Programming

Transcript

  1. jshell - some features Trying out snippets of code Tab

    completion for snippets and commands Changing definitions Forward references Editing and searching in shell, like you expect External editor with /edit Loading external code through classpath or module path Saving and loading external scripts 3 / 40
  2. Some jshell commands /exit - first thing to know is

    how to get out (^D also works) /help - list of available commands /list - lists all snippets /save /open - save and open snippets to and from disk /vars /types /methods - list current vars etc 4 / 40
  3. Collections (old school) Set<String> set = new HashSet<>(); set.add("Java"); set.add("eight");

    set.add("collections"); set = Collections.unmodifiableSet(set); 6 / 40
  4. Collections (old school) Set<String> set = new HashSet<>(); set.add("Java"); set.add("eight");

    set.add("collections"); set = Collections.unmodifiableSet(set); Set<String> set = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("Java", "eight", "collections"))); 7 / 40
  5. Collections (old school) Set<String> set = new HashSet<>(); set.add("Java"); set.add("eight");

    set.add("collections"); set = Collections.unmodifiableSet(set); Set<String> set = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("Java", "eight", "collections"))); Set<String> set = Collections.unmodifiableSet(new HashSet<String>() {{ add("Java"); add("eight"); add("collections"); }}); 8 / 40
  6. Collections (old school) Set<String> set = new HashSet<>(); set.add("Java"); set.add("eight");

    set.add("collections"); set = Collections.unmodifiableSet(set); Set<String> set = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("Java", "eight", "collections"))); Set<String> set = Collections.unmodifiableSet(new HashSet<String>() {{ add("Java"); add("eight"); add("collections"); }}); Set<String> set = Collections.unmodifiableSet(Stream.of("Java", "eight", "collections").collect(toSet())); 9 / 40
  7. Collections (old school) Set<String> set = new HashSet<>(); set.add("Java"); set.add("eight");

    set.add("collections"); set = Collections.unmodifiableSet(set); Set<String> set = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("Java", "eight", "collections"))); Set<String> set = Collections.unmodifiableSet(new HashSet<String>() {{ add("Java"); add("eight"); add("collections"); }}); Set<String> set = Collections.unmodifiableSet(Stream.of("Java", "eight", "collections").collect(toSet())); Guava anyone?? 10 / 40
  8. New collection factory methods Set<String> mySet = Set.of("Java", "nine", "collections");

    List<String> myList = List.of("Java", "nine", "collections"); 12 / 40
  9. New collection factory methods Set<String> mySet = Set.of("Java", "nine", "collections");

    List<String> myList = List.of("Java", "nine", "collections"); Map<String,String> myMap = Map.of("Duke", "Java", "coolest", "nine", "better", "collections"); 13 / 40
  10. New collection factory methods Set<String> mySet = Set.of("Java", "nine", "collections");

    List<String> myList = List.of("Java", "nine", "collections"); Map<String,String> myMap = Map.of("Duke", "Java", "coolest", "nine", "better", "collections"); Map<String,String> myEntryMap = Map.ofEntries( Map.entry("Duke", "Java"), Map.entry("coolest", "nine"), Map.entry("better", "collections") ); 14 / 40
  11. Such collections Are structurally immutable (mutators throw UnsupportedOperationException) Disallow null

    elements (and keys) Are serializable if all elements (and keys) are Sets and Maps throw IllegalArgumentException for duplicate elements/keys at creation time Iteration order of Lists behave as expected and is undefined for Sets and Maps Are value based (you should not rely on ==) 15 / 40
  12. New methods on Optional public void ifPresentOrElse (Consumer<? super T>

    action, Runnable emptyAction) public Optional<T> or (Supplier<? extends Optional<? extends T>> supplier) 18 / 40
  13. New methods on Optional public void ifPresentOrElse (Consumer<? super T>

    action, Runnable emptyAction) public Optional<T> or (Supplier<? extends Optional<? extends T>> supplier) public Stream<T> stream () 19 / 40
  14. New methods on Optional public void ifPresentOrElse (Consumer<? super T>

    action, Runnable emptyAction) public Optional<T> or (Supplier<? extends Optional<? extends T>> supplier) public Stream<T> stream () List<Optional<String>> optionals = List.of( Optional.of("Java"), Optional.of("nine"), Optional.empty(), Optional.of("Optional") ); System.out.println("Optionals in list:") optionals.stream().forEach(System.out::println) System.out.println("All Optional values (or null):") optionals.stream().map(o -> o.orElse(null)).forEach(System.out::println) System.out.println("Only present Optional values:") optionals.stream().flatMap(Optional::stream).forEach(System.out::println) 20 / 40
  15. I/O and resources "Effectively final" variables allowed in try-with-resources void

    copyFile(InputStream in, OutputStream out) throws IOException { try (in; out) { in.transferTo(out); } } InputStream inFile = Files.newInputStream(Paths.get("in.txt")); OutputStream outFile = Files.newOutputStream(Paths.get("out.txt")); copyFile(inFile, outFile); try (InputStream inFileAgain = Files.newInputStream(Paths.get("in.txt"));) { byte[] allBytes = inFileAgain.readAllBytes(); } 22 / 40
  16. I/O and resources New methods on InputStream void copyFile(InputStream in,

    OutputStream out) throws IOException { try (in; out) { in.transferTo(out); } } InputStream inFile = Files.newInputStream(Paths.get("in.txt")); OutputStream outFile = Files.newOutputStream(Paths.get("out.txt")); copyFile(inFile, outFile); try (InputStream inFileAgain = Files.newInputStream(Paths.get("in.txt"));) { byte[] allBytes = inFileAgain.readAllBytes(); } 23 / 40
  17. New HTTP client in Java! Supports HTTP/2 and WebSockets Supports

    sync and async request modes Supports multiple HTTP/2 push responses Replaces the legacy HttpURLConnection 25 / 40
  18. Caveats Incubator module Not all (expected) features currently available Apparently

    vulnerable to attacks (BREACH and CRIME) Apparently sloppy about cookie security HTTP/1.1 encryption 26 / 40
  19. HttpClient Example import jdk.incubator.http.*; HttpClient.newHttpClient() .sendAsync( HttpRequest.newBuilder(URI.create("https://www.google.no")) .GET() .build(), HttpResponse.BodyHandler.asString())

    .thenAccept(response -> System.out.println(response.statusCode())); Must add incubator module to jshell When starting: $ jshell --add-modules jdk.incubator.httpclient Or inside jshell jshell> /env -add-modules jdk.incubator.httpclient 28 / 40
  20. New deprecations Applet API Observer and Observable boxed primitive constructors

    Object.finalize java.security.acl API com.sun.jarsigner package policytool 31 / 40
  21. Stu to be gone CORBA sun.misc.Unsafe.defineClass pre 1.2 SecurityManager methods

    java.activation java.se.ee java.transaction (subset of JTA to support CORBA interoperation) java.xml.bind java.xml.ws java.xml.ws.annotation 32 / 40
  22. Stu gone in 9 _ as identifier hprof and jhat

    tools Demos and Samples 33 / 40
  23. Streams Stream (and primitive streams) now have takeWhile(Predicate) dropWhile(Predicate) //Expected

    0,1,2,3,4 IntStream.of(0,1,2,3,4,5,6,7,8,9).takeWhile(i -> i < 5).forEach(System.out::println) //Expected 5,6,7,8,9 IntStream.of(0,1,2,3,4,5,6,7,8,9).dropWhile(i -> i < 5).forEach(System.out::println) 35 / 40
  24. Multi-release JARs jar root - A.class - B.class - C.class

    - D.class - META-INF - versions - 9 - A.class - B.class - 10 - A.class 36 / 40
  25. Private methods in interfaces interface Greeter { ... default void

    greetSomeone(String personName, boolean female) { System.out.println("Hi " + getTitle(female) + " " + personName); } default void farewellSomeone(String personName, boolean female) { System.out.println("Bye " + getTitle(female) + " " + personName); } private String getTitle(boolean female) { return female ? "Ms" : "Mr"; } } 37 / 40
  26. Finally Process import statements correctly javac --release N (no more

    source and target versions, rt.jar etc) Flow class (with Publisher, Subscriber, Processor, Subscription for reactive streams) HTML5 JavaDoc (with search) UDP security (DTLS) UTF-8 property files and Unicode 7.0 support New version string schema for all JDK $MAJOR.$MINOR.$SECURITY.$PATCH 38 / 40
  27. Resources and inspiration JavaZone 2017: Java SE 9, and its

    hidden treasures walkthrough https://docs.oracle.com/javase/9/jshell/toc.htm http://openjdk.java.net/projects/jdk9/ http://www.oracle.com/technetwork/java/javase/9-deprecated-features-3745636.html https://docs.oracle.com/javase/9/docs/api/overview-summary.html https://dzone.com/articles/java-9-besides-modules https://dzone.com/articles/java-9-http-20 40 / 40