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

Welcome to Java 9

Welcome to Java 9

I gave this presentation at Spring Days NYC in 2017. It covers what is new in Java 9 besides Jigsaw.

Jeanne Boyarsky

June 20, 2017
Tweet

More Decks by Jeanne Boyarsky

Other Decks in Technology

Transcript

  1. Welcome to Java 9 Spring Days – 6/20/17 Jeanne Boyarsky

    https://www.slideshare.net/ boyarsky/2017-java9springdays Twitter @jeanneboyarsky Blog: http://www.selikoff.net
  2. Modules (Jigsaw) Problem •  Jar hell •  Solution – Maven/Gradle?

    •  No closed/private packages Java 9 •  Modules! Twitter: @jeanneboyarsky
  3. Jigsaw – flux •  You can use a command line

    flag. •  The flag might or might not be the default. Twitter: @jeanneboyarsky Do you •  use sun.* internal APIs? •  use a jar that does?
  4. •  2011 – Maybe part of Java 7. Nope Plan

    B •  2012 - Part of Java 8. No wait •  2014 - Part of Java 9 •  Sept 2016 •  May 2017 •  July 2017 •  Sept 2017? The Saga of Jigsaw Twitter: @jeanneboyarsky
  5. Creating a non-empty Set Set<String> set = new HashSet<>( Arrays.asList("1","2",

    "3")); Set<String> set = Stream.of("1", "2”, "3") .collect(Collectors.toSet()); Set<String> set = Set.of("1", "2", "3"); Twitter: @jeanneboyarsky
  6. For consistency Map<String, String> map = Map.of("k", "v"); Up to

    10 params, then varargs List<String> list = List.of("1", "2", "3"); Up to 20 params, then varargs Twitter: @jeanneboyarsky
  7. Try with Resources Path path = Paths.get("test.txt"); try (BufferedReader reader

    = Files.newBufferedReader(path)) { System.out.println(reader.readLine()); } Effectively final resources Twitter: @jeanneboyarsky BufferedReader reader = Files.newBufferedReader(path); try (reader) { System.out.println(reader.readLine()); }
  8. What’s wrong here? Connection con = DriverManager.getConnection(url); PreparedStatement ps =

    con.prepareStatement(sql); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); try (con; ps; rs) { while (rs.next()) { // process result set } } Twitter: @jeanneboyarsky Resource leak!
  9. @Deprecated “Very few deprecated APIs were actually removed, leading some

    people to believe that nothing would ever be removed. On the other hand, other people believed that everything that was deprecated might eventually be removed, which was never the intent either.” From JEP 277 Twitter: @jeanneboyarsky
  10. @Deprecated New Attributes Attribute Type Description forRemoval boolean Is the

    intent to remove API from Java at some point? since String Version of Java when API first became deprecated (not populated for all pre-Java 9 APIs) Twitter: @jeanneboyarsky Only had for new APIs before
  11. What is deprecated for removal? •  Unused code in AWT

    •  Some old security APIs •  Some thread and runtime APIs •  Some Jigsaw transition modules (but not classes) •  And… Twitter: @jeanneboyarsky
  12. Streams - takeWhile Stream.iterate(entry(1,1), e -> entry(e.getValue(), e.getKey()+e.getValue())) .map(Entry::getValue) .takeWhile(n

    -> n < 30) .forEach(System.out::println); How print all Fibonacci #s less than 30? Assumes ordered stream. Takes all elements until one doesn’t match. Twitter: @jeanneboyarsky
  13. Streams - dropWhile Stream.iterate(entry(1,1), e -> entry(e.getValue(), e.getKey()+e.getValue())) .map(Entry::getValue) .dropWhile(n

    -> n < 30) .forEach(System.out::println); How print all Fibonacci #s greater than 30? Note: This doesn’t work. takeWhile and dropWhile aren’t always opposites. See why? Twitter: @jeanneboyarsky
  14. Streams - ofNullable stream = dubiousObj == null ? Stream.empty()

    : Stream.of(dubiousObj); stream = Stream.ofNullable(dubiousObj); Twitter: @jeanneboyarsky
  15. JShell •  REPL for Java •  Now with tab and

    up arrow •  More packages known than Nashorn Twitter: @jeanneboyarsky
  16. Random other changes •  Can now have private methods in

    interfaces •  _ is no longer a legal identifier. Still ok to have vars like _temp •  sun.misc.Unsafe was replaced by VarHandle. I can’t imagine needing to do either. Maybe for low level APIs •  Could use @SafeVarargs on static methods or final instance methods. Now can do the same for private instance methods Twitter: @jeanneboyarsky
  17. More new APIs APIs for reactive programming: • Flow.Processor • Flow.Publisher • Flow.Subscriber

    • Flow.Subscription (new class Flow) Process APIs Process.pid() supportsNormalTermination() CompletableFuture<Process> onExit() info() descendants() children() Twitter: @jeanneboyarsky