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
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Java 9 Modules and New Features
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. Moving on
That was nerve wracking.
We clearly need sugar!
Twitter: @jeanneboyarsky
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. 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
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. @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. @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
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
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. 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
22. JShell
• REPL for Java
• Now with tab and up arrow
• More packages known than Nashorn
Twitter: @jeanneboyarsky
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. 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