+ All Categories
Home > Software > engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

Date post: 16-Apr-2017
Category:
Upload: rene-winkelmeyer
View: 540 times
Download: 2 times
Share this document with a friend
65
1 Get ready for moving from Java 6 to Java 8 – Now! René Winkelmeyer 24.03.2016
Transcript
Page 1: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

1

Get ready for moving from Java 6 to Java 8 – Now!

René Winkelmeyer 24.03.2016

Page 2: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

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

Page 3: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

3

YES – I CAN!

Page 4: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

4

A timeline perspective

§  Quick poll

§  IBM Notes/Domino development?

§  WebSphere development?

§  Java 6 only development?

§  Java 7+ only development?

§  Mixed Java version development?

Page 5: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

5

A timeline perspective

Page 6: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

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

Page 7: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

7

What this session covers

§  Switch-strings

§  Diamonds

§  Paths

§  Lambdas

§  Streams

§  Date/Time

Page 8: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

8

Goodbye if-then-elseif-elseif-elseif… for Strings

Page 9: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

9

Goodbye if-then-elseif-elseif-elseif… for Strings

Way in Java 6 and smaller to check for different String types

Page 10: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

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

Page 11: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

11

Goodbye if-then-elseif-elseif-elseif… for Strings

Now a clean way. Alternative could be to use Enums (also prior Java 7).

Page 12: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

12

Diamonds are developers best friends

Page 13: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

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

Page 14: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

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. ;-)

Page 15: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

15

Automated resource management

Page 16: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

16

Automated resource management (simple)

Close in finally

Multiple exceptions to catch

Init outside the try to close it later

Page 17: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

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!

Page 18: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

18

Automated resource management (simple)

Close in finally

Multiple exceptions to catch

Init outside the try to close it later

Page 19: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

19

Automated resource management (simple)

No finally, no close()

Page 20: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

20

Automated resource management (more)

Multiple streams to close

Multiple streams to handle

Multiple streams to close

Page 21: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

21

Automated resource management (more)

„Join“ multiple AutoCloseables with a ; separator within the try statement

Page 22: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

22

Underscore me

Page 23: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

23

Underscore me

§  You‘ve two seconds to identify these numbers

Page 24: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

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_)

Page 25: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

25

Underscore me

§  You‘ve two seconds to identify these numbers

Page 26: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

26

Underscore me

§  You‘ve two seconds to identify these n_e_w numbers

Page 27: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

27

Catch me if you can

Page 28: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

28

Catch me if you can

One catch per named exception

Page 29: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

29

Catch me if you can

multi-catch has been introduced with Java 7

Page 30: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

30

NIO.2

Page 31: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

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

Page 32: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

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

Page 33: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

33

Make your Path (and more)

Page 34: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

34

Make your Path (and more)

Page 35: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

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.

Page 36: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

36

Watch your files

Holds the results of the monitored events

Map of all found events

Page 37: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

37

default

Page 38: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

38

default

§  Changes in Interfaces always led to changes in the classes they used them

§  The new default constructor introduces „backward“ compatibility

Page 39: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

39

Before Java 8

Page 40: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

40

Before Java 8

Adding a new Interface method

Compilation error on all classes that implement the Interface

Page 41: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

41

With Java 8

Method body needed

Page 42: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

42

With Java 8

Calling a duplicate default method

Page 43: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

43

Lambdas

Page 44: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

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!

Page 45: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

45

Lambdas

Standard boilerplate code to iterate Sequential, not parallel!

Page 46: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

46

Lambdas

Anonymous methods to the rescue but ...

Have you spotted the forEach?

Page 47: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

47

Lambdas

Calling the anonymous inner method directly via a lambda call

Page 48: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

48

Lambdas

Shorten it even more by omitting the casting

Page 49: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

49

Lambdas

Page 50: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

50

Lambdas

Page 51: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

51

Streams

Page 52: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

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.

Page 53: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

53

Available stream functions

Source:http://zeroturnaround.com/rebellabs/java-8-streams-cheat-sheet/attachment/java-8-streams-cheat-sheet-v3/

Page 54: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

54

Streams

Calling the stream

Termination call

Page 55: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

55

Streams and Lambdas

Much cleaner implementation using Streams and Lambdas

Obey the new lines – better for debugging...

Page 56: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

56

Streams – a little bit more complete

Output: C1 C2

Page 57: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

57

Streams – a little bit more complete

Source:http://zeroturnaround.com/rebellabs/java-8-streams-cheat-sheet/attachment/java-8-streams-cheat-sheet-v3/

Page 58: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

58

Date and Time

Page 59: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

59

Date and Time

§  Who loves Date and Time operations in Java?

§  Who is using Joda Time?

Page 60: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

60

Date and Time – Spot the error

+1900 0-based

Page 61: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

61

Date and Time – Basic Concept

§  LocalDate, LocalTime, LocalDateTime

§  Instant (amount of time since the epoch, i. e. for timestamps)

§  Period

§  Duration

Page 62: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

62

Date and Time – Examples

§  Now -> LocalDate.now()

§  Static -> LocalDate(2013, Month.JANUARY, 21)

§  Parse -> LocalDate.parse()

§  Conversion -> Calendar.getInstance().toInstant()

Page 63: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

63

Date and Time – Examples

Page 64: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

64

Date and Time – Examples

Page 65: engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!

65

Q & A!


Recommended