java8

26
© by mimacom ag 05/05/2014

Upload: jaime-l-lopez-carratala

Post on 14-Aug-2015

29 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Java8

© by mimacom ag 05/05/2014

Page 2: Java8

© mimacom ag

Summary

2

• New features and concepts

• New and extended APIs

• Performance improvements

• Conclusion

• Q & A

Page 3: Java8

© mimacom ag

New features and concepts

3

• Interface changes

• Lambda expressions

• Streams

• Nashorn

• Java FX improvements

Page 4: Java8

© mimacom ag

Interface changes

4

• Interfaces can now define static methods:

• Interfaces can now define default methods:

Page 5: Java8

© mimacom ag

Interface changes

5

• Functional interfaces:• An interface is functional if it defines exactly one abstract

method.

• Default and static methods are not abstract, so they can be

defined without compile errors.

• This annotation has been introduced to declare interfaces as

functional: @FunctionalInterface

• Runnable, Comparator or Cloneable are some examples of

functional interfaces.

Page 6: Java8

© mimacom ag

Lambda expressions

6

• Functional Interfaces + Lambda Probably the biggest feature

since generics.

• They are basically used to instantiate functional interfaces, for

instance:

Page 7: Java8

© mimacom ag

Lambda expressions

7

• Concrete examples:

Page 8: Java8

© mimacom ag

Lambda expressions

8

• A lambda is compatible with a functional interface when their

inputs, output and checked exceptions match.

• Lambdas can capture variables outside the lambda body if the

variables are effectively final.

• Lambdas can’t be used to control the flow of the application:

Page 9: Java8

© mimacom ag

Lambda expressions

9

• Advantadges:

• Less code to write.

• Flexibility to instantiate interfaces.

• Alternative to anonymous inner classes.

• Disadvantages:

• More difficult to debug.

Page 10: Java8

© mimacom ag

Streams

10

• A sequence of elements supporting sequential and parallel

aggregate operations.

• A stream can be used just once.

• The most common way to obtain a stream is from collections:

Page 11: Java8

© mimacom ag

Streams

11

• Two kind of operations:

• Intermadiate: Keep the stream open and allow to concatenate

operations with the same stream.

• Some of them: Filter, map, flatMap, peek, distinct, sorted,

etc.

• Terminal: It must be the final operation, once this one is

executed, the stream is consumed and is no longer available.

• Some of them: forEach, toArray, reduce, collect, sum, min,

max, etc.

Page 12: Java8

© mimacom ag

Streams

12

• A couple of examples:

Page 13: Java8

© mimacom ag

Nashorn

13

• New JS engine, Rhino’s successor.

• Ability to access Java classes from JS… and

viceversa.

• Nashorn + Avatar.js Node.js functionality

Page 14: Java8

© mimacom ag

Nashorn

14

• Why use Nashorn (extracted from Oracle):• Can reuse existing Java libraries and middleware

• Can take advantage of multiple cores

• Can be secured using the Java security model

• Can be managed using existing Java tools

• VisualVM, MBeans, Flight Recorder, Mission Control

• Can access Java Platform APIs to complement node APIs

• BigInteger, Date, Calendar, XML, SQL, Multicast, etc

• Small enough for embedded usage (less than 2Mb)

Page 15: Java8

© mimacom ag

JavaFX improvements

15

• It is the sustitute of Swing and it has been

improved in java 8.

• New look with Modena theme.

Page 16: Java8

© mimacom ag

JavaFX improvements

16

• True 3D support.

• Video & audio recording.

• Printing support

• Added new controls

• Fully open source.

Page 17: Java8

© mimacom ag

New and extended APIs

17

• New java.time API

• java.util.function

• Collection API additions

• Other additions

Page 18: Java8

© mimacom ag

New java.time API

18

• Similar to JodaTime library.

• Instead of include JodaTime in the JDK, it was

redesigned from scratch.

• Useful value types:• LocalDateTime, LocalDate, LocalTime.

• Year, Month, YearMonth. MonthDay, DayOfWeek.

• Instant, Period, ZonedDateTime.

Page 19: Java8

© mimacom ag

Java.util.function

19

• Set of functional interfaces widely used in the new APIs of Java 8.

• Here are a few:

• Function<T, R> - take a T as input, return an R as ouput.

• Predicate<T> - take a T as input, return a boolean as output.

• Consumer<T> - take a T as input, perform some action and

don't return anything.

• Supplier<T> - with nothing as input, return a T.

• BinaryOperator<T> - take two T's as input, return one T as

output, useful for "reduce" operations.

Page 20: Java8

© mimacom ag

Colletion API additions

20

• The ability that interfaces can define default methods have

introduced a lot of additionson the colletion interfaces. Some of

them:

• Iterable.forEach(Consumer)

• Iterator.forEachRemaining(Consumer)

• Collection.removeIf(Predicate)

• Collection.spliterator()

• List.sort(Comparator)

• List.replaceAll(UnaryOperator)

• Map.forEach(BiConsumer)

• …

Page 21: Java8

© mimacom ag

Other additions

21

• There are several additions with a lot of new classes and new

methods:

• Concurrency API additions.

• IO/NIO API additions.

• New methods and classes in java.lang, java.util and in several places more.

Page 22: Java8

© mimacom ag

Performance improvements

22

Page 23: Java8

© mimacom ag

Conclusion

23

• A lot of additions in this release.

• Lambdas and functional interfaces will change a little bit the way

people is developing.

• But several of them were already in antoher languages, so Oracle

has not invented nothing new.

• It seems that they have worked really hard to improve

performance, let’s see if it is true!!!

Page 24: Java8

© mimacom ag

24

References

• http://docs.oracle.com/javase/8/docs/api/overview-summary.html

• http://www.techempower.com/blog/2013/03/26/everything-about-

java-8/

• http://www.infoq.com/articles/nashorn

• https://avatar.java.net/

• http://java.dzone.com/articles/whats-new-java-8-part-i-javafx

• https://blogs.oracle.com/thejavatutorials/entry/

learn_more_about_performance_and

Page 25: Java8

© mimacom ag

25

Page 26: Java8

© mimacom ag

customer orientedUser friendlyCompetentlyQualitatively

Efficient

26

…the open source integrator