Upload
rene-winkelmeyer
View
540
Download
2
Embed Size (px)
Citation preview
1
Get ready for moving from Java 6 to Java 8 – Now!
René Winkelmeyer 24.03.2016
2
• Skype / Twitter / LinkedIn / Slideshare
muenzpraeger
• Web https://blog.winkelmeyer.com
• Mail [email protected]
OpenNTF • File Navigator • Generic NSF View Widget for IBM Connections
About me
René Winkelmeyer Head of Development
3
YES – I CAN!
4
A timeline perspective
§ Quick poll
§ IBM Notes/Domino development?
§ WebSphere development?
§ Java 6 only development?
§ Java 7+ only development?
§ Mixed Java version development?
5
A timeline perspective
6
A timeline perspective
Java 6 23.12.2006
Java 7 28.07.2011
Java 8 18.03.2014
Java 9 23.03.2017
Java versions by release date
IBM Notes/Domino
IBM WebSphere (Full Profile)
IBM WebSphere (Liberty Profile)
Supported Java
version by product
7
What this session covers
§ Switch-strings
§ Diamonds
§ Paths
§ Lambdas
§ Streams
§ Date/Time
8
Goodbye if-then-elseif-elseif-elseif… for Strings
9
Goodbye if-then-elseif-elseif-elseif… for Strings
Way in Java 6 and smaller to check for different String types
10
Goodbye if-then-elseif-elseif-elseif… for Strings
§ Java 7 added support for java.lang.String in switch
§ Increased readability
§ Produces more efficient byte code
§ Switching based on the hashcode()
§ May (still) throw a NullPointerException
11
Goodbye if-then-elseif-elseif-elseif… for Strings
Now a clean way. Alternative could be to use Enums (also prior Java 7).
12
Diamonds are developers best friends
13
Diamonds are developers best friend
§ Java 7 introduced the usage of the <> operator for type inference
§ Removes verbose code
§ Makes usage of 3rd party libs like Google Guava for List creation obsolete
14
Diamonds are developers best friend
Raw error warning in Eclipse. Technically possible, but not recommended.
How to do it. Imagine HashMap<String, HashMap<String, Object>> i. e.
Diamond operator for less code writing. ;-)
15
Automated resource management
16
Automated resource management (simple)
Close in finally
Multiple exceptions to catch
Init outside the try to close it later
17
Automated resource management
§ Java 7 introduced the new java.lang.AutoCloseable interface
§ Resources that inherit from this interface are automatically closed when created within a try statement
§ Reduces boilerplate code drastically
§ java.lang.Closeable extends from java.lang.AutoCloseable
§ AutoCloseable.close() shouldn‘t be called twice!
18
Automated resource management (simple)
Close in finally
Multiple exceptions to catch
Init outside the try to close it later
19
Automated resource management (simple)
No finally, no close()
20
Automated resource management (more)
Multiple streams to close
Multiple streams to handle
Multiple streams to close
21
Automated resource management (more)
„Join“ multiple AutoCloseables with a ; separator within the try statement
22
Underscore me
23
Underscore me
§ You‘ve two seconds to identify these numbers
24
Underscore me
§ The with Java 7 introduced capability allows to add underscores to numeric types (int, double, long).
§ A few rules to obey:
§ Can be used after the decimal point, but not directly before/after it
§ No underscores at the end or the beginning (i. e. _5000 or 5000_)
25
Underscore me
§ You‘ve two seconds to identify these numbers
26
Underscore me
§ You‘ve two seconds to identify these n_e_w numbers
27
Catch me if you can
28
Catch me if you can
One catch per named exception
29
Catch me if you can
multi-catch has been introduced with Java 7
30
NIO.2
31
NIO.2
§ The handling of file system related actions has been much improved with Java 7.
§ Removes the need in a lot of cases to rely on 3rd pary libraries like Apache Commons Files.
§ Direct implementation in the base JDK
32
NIO.2
§ java.nio.Path
§ Think of it as a replacement for java.io.File
§ Represents a file or a folder – and it doesn‘t need to exist (no Exception ;-))
§ java.nio.Files
§ Loads of utility classes for file and folder handling
33
Make your Path (and more)
34
Make your Path (and more)
35
Watch your files
Initiates the default WatchService as introduced with Java 7
Assign the to be monitored path using the new java.nio.Path API
Register the created WatchService with the path. Events that can be monitored are create, update and delete.
36
Watch your files
Holds the results of the monitored events
Map of all found events
37
default
38
default
§ Changes in Interfaces always led to changes in the classes they used them
§ The new default constructor introduces „backward“ compatibility
39
Before Java 8
40
Before Java 8
Adding a new Interface method
Compilation error on all classes that implement the Interface
41
With Java 8
Method body needed
42
With Java 8
Calling a duplicate default method
43
Lambdas
44
Lambdas
§ One of the biggest changes with Java 8
§ Known from other languages like Closure, Scala, C# and more
§ Defined in Java Specification Request (JSR) 335
§ Knows as closures or functional literals in other languages
§ Love or hate them – there‘s no way around them!
45
Lambdas
Standard boilerplate code to iterate Sequential, not parallel!
46
Lambdas
Anonymous methods to the rescue but ...
Have you spotted the forEach?
47
Lambdas
Calling the anonymous inner method directly via a lambda call
48
Lambdas
Shorten it even more by omitting the casting
49
Lambdas
50
Lambdas
51
Streams
52
What is a stream and what not?
§ It is a pipeline of functions that can be evaluated.
§ They are not a data structure.
§ They can transform data – but cannot mutate them.
53
Available stream functions
Source:http://zeroturnaround.com/rebellabs/java-8-streams-cheat-sheet/attachment/java-8-streams-cheat-sheet-v3/
54
Streams
Calling the stream
Termination call
55
Streams and Lambdas
Much cleaner implementation using Streams and Lambdas
Obey the new lines – better for debugging...
56
Streams – a little bit more complete
Output: C1 C2
57
Streams – a little bit more complete
Source:http://zeroturnaround.com/rebellabs/java-8-streams-cheat-sheet/attachment/java-8-streams-cheat-sheet-v3/
58
Date and Time
59
Date and Time
§ Who loves Date and Time operations in Java?
§ Who is using Joda Time?
60
Date and Time – Spot the error
+1900 0-based
61
Date and Time – Basic Concept
§ LocalDate, LocalTime, LocalDateTime
§ Instant (amount of time since the epoch, i. e. for timestamps)
§ Period
§ Duration
62
Date and Time – Examples
§ Now -> LocalDate.now()
§ Static -> LocalDate(2013, Month.JANUARY, 21)
§ Parse -> LocalDate.parse()
§ Conversion -> Calendar.getInstance().toInstant()
63
Date and Time – Examples
64
Date and Time – Examples
65
Q & A!