Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

2017 java9-spring-days

364 views

Published on

This Java 9 presentation is from Spring Days NY 2017. It covers what is new in Java 9 aside from modules/lambdas. It also gives an update on the current state of lambdas

Published in: Technology
  • Be the first to comment

2017 java9-spring-days

  1. 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. 2. Modules (Jigsaw) Problem • Jar hell • Solution – Maven/Gradle? • No closed/private packages Java 9 • Modules! Twitter: @jeanneboyarsky
  3. 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. 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. 5. Moving on That was nerve wracking. We clearly need sugar! Twitter: @jeanneboyarsky
  6. 6. 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
  7. 7. 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
  8. 8. Better Map Twitter: @jeanneboyarsky Map<Integer, Integer> map = Map.ofEntries( Map.entry(1, 10), Map.entry(2, 20) );
  9. 9. JavaDoc Search Upgrade Twitter: @jeanneboyarsky (to 2017)
  10. 10. 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()); }
  11. 11. 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!
  12. 12. @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
  13. 13. @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
  14. 14. Twitter: @jeanneboyarsky
  15. 15. @Deprecated Warning types • Ordinary Deprecation • Removal @SuppressWarnings("deprecation”) @SuppressWarnings("removal”) @SuppressWarnings({"deprecation", "removal"}) Twitter: @jeanneboyarsky
  16. 16. 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
  17. 17. Applets Finally deprecated! But… forRemoval=false Applet Twitter: @jeanneboyarsky
  18. 18. 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
  19. 19. 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
  20. 20. Streams - iterate Stream.iterate(10, i-> i-1) .limit(10) .forEach(System.out::println); Stream.iterate(10, i-> i>0, i-> i-1) .forEach(System.out::println); Twitter: @jeanneboyarsky
  21. 21. Streams - ofNullable stream = dubiousObj == null ? Stream.empty() : Stream.of(dubiousObj); stream = Stream.ofNullable(dubiousObj); Twitter: @jeanneboyarsky
  22. 22. JShell • REPL for Java • Now with tab and up arrow • More packages known than Nashorn Twitter: @jeanneboyarsky
  23. 23. 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
  24. 24. 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
  25. 25. Questions ? Twitter: @jeanneboyarsky

×