Top Banner
1 CONFIDENTIAL JAVA 8 by Sergii Stets
33

Java 8 - Features Overview

Feb 11, 2017

Download

Education

Sergii Stets
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java 8 - Features Overview

1CONFIDENTIAL

JAVA 8

by Sergii Stets

Page 2: Java 8 - Features Overview

2CONFIDENTIAL

• Java 8 General Overview• Lambdas and Functional Interfaces• Stream API• Method References• Interface’s Default and Static Methods • Optional• Date/Time API• Nashorn, JS Engine

Agenda

Page 3: Java 8 - Features Overview

3CONFIDENTIAL

Java installing problems

Please, pay attention when installing Java 8. Note! Tomcat 7.0.x does not work with Java 8:

So make sure your JAVA_HOME is not pointing to Java 8 SDK directory or you will get bunch of errors. Change your JAVA_HOME or configure your project properly.

Page 4: Java 8 - Features Overview

4CONFIDENTIAL

Overview

JAVA 8 RELEASE (March 18, 2014)

Optional (value wrapper)Lambdas & Method references

METHODS INSIDE JAVA INTERFACES

VALUE

Default methods New Utilities

Stream API Date Time API Nashorn, JS Engine

Page 5: Java 8 - Features Overview

LAMBDAS EXPRESSIONS

Page 6: Java 8 - Features Overview

6CONFIDENTIAL

Lambdas (closures) syntaxEach lambda expression has two parts separated by an arrow token: the left hand side is our method arguments, and the right hand side is what we do with those arguments (business logic)

Page 7: Java 8 - Features Overview

7CONFIDENTIAL

Just compare

And it’s not even about lines of code

Page 8: Java 8 - Features Overview

8CONFIDENTIAL

Motivation

• Shorter way of writing an implementation of a method for later execution.

• Provides simple horizontal solution that solves the "vertical problem" presented by inner classes.

• Lambda expressions are designed to allow code to be streamlined.

• Lambdas expressions do not produce any extra classes the way nested classes do.

Page 9: Java 8 - Features Overview

9CONFIDENTIAL

Method reference

Oracle reference guide

“You use lambda expressions to create anonymous methods. Sometimes, however, a lambda expression does nothing but call an existing method. In those cases, it's often clearer to refer to the existing method by name.” 

Kind Example

Reference to a static method ContainingClass::staticMethodName

Reference to an instance method of a particular object containingObject::instanceMethodName

Reference to an instance method of an arbitrary object of

a particular typeContainingType::methodName

Reference to a constructor ClassName::new

Kinds of methods referencesExample

Page 10: Java 8 - Features Overview

10CONFIDENTIAL

Functional Interfaces

Conceptually, a functional interface has exactly one abstract method

(e.g. Runnable, Callable, Comparator).

Page 11: Java 8 - Features Overview

11CONFIDENTIAL

Pre-Built function library (java.util.function)

There are a lot of re-usable functional requirements that can be captured by functional interfaces and lambdas. The designers of Java 8 have captured the common use cases and created a library of functions

… etc.

Page 12: Java 8 - Features Overview

STREAMS API

Page 13: Java 8 - Features Overview

13CONFIDENTIAL

Hello, Streams

Streams let you process data in a declarative way. We do not say how to process the data, we just say what to do with the data.

Let’s say we need to find all transactions of type ‘grocery’ and return a list of transaction IDs sorted in decreasing order of transaction value.

Page 14: Java 8 - Features Overview

14CONFIDENTIAL

java.util.stream.Stream: feel the power.

It takes 10 seconds to process 10 filesIt takes 1 second to process 10 files

Page 15: Java 8 - Features Overview

15CONFIDENTIAL

Streams versus Collections

Q. What is the difference?

Q. So when should I use streams and why?

Q. On demand?

A. In a nutshell, collections are about data and streams are about computations.

A. Use streams when things are computed. A collection is an in-memory data structure, which holds all the values that the data structure currently has—every element in the collection has to be computed before it can be added to the collection. In contrast, a stream is a conceptually fixed data structure in which elements are computed on demand.

A. Yes, look: limit(2) uses short-circuiting; we need to process only part of the stream, not all of it.

Page 16: Java 8 - Features Overview

16CONFIDENTIAL

So…

Stream as an abstraction for expressing efficient, SQL-like operations on a collection of data.

The Streams API will internally decompose your query to leverage the multiple cores on your computer.

- LIKE

Page 17: Java 8 - Features Overview

OPTIONAL VALUE

Page 18: Java 8 - Features Overview

18CONFIDENTIAL

Optional

A. “… a container object which may or may not contain a non-null value...” Java 8 Reference Guide

Q. Of course, the null reference is the source of many problems. But how does Optional solve them?

Q. What is it?

Q. Why do we need them? A. Because "Null sucks."  Doug Le (a professor of computer science)

A. Enough talking; let's see some code!

Page 19: Java 8 - Features Overview

19CONFIDENTIAL

java.util.Optional usage

Do Something If a Value Is Present

Default Values and Actions

Creating of OptionalOld style

New style

Old style

New style

Rejecting Certain Values Using the filter MethodOld style

New style

Page 20: Java 8 - Features Overview

METHODSINSIDE JAVA INTERFACES

Page 21: Java 8 - Features Overview

21CONFIDENTIAL

What are default methods?

Default methods enable you to add new functionality to the interfaces of your libraries and ensure backward compatibility with code written for older versions of those interfaces.

The forEach isn’t declared by java.util.List nor the java.util.Collection interface yet. The solution would be to just add the new method to the existing interface and provide the implementation where required in the JDK. But, once published, it is impossible to add methods to an interface without breaking the existing implementation

Syntax

Page 22: Java 8 - Features Overview

22CONFIDENTIAL

Extending interfaces with default methods

When you extend an interface that contains a default method, you can do the following:

Not mention the default method at all, which lets inherit the default method.

Redeclare the default method, which makes it abstract.

Redefine the default method, which overrides it.

Page 23: Java 8 - Features Overview

23CONFIDENTIAL

Tricks

“What if the class implements two interfaces and both those interfaces define a default method with the same signature?”

Solution #1 – Override

Solution #2 - Reference

Page 24: Java 8 - Features Overview

24CONFIDENTIAL

Static methods

Like static methods in classes, you specify that a method definition in an interface is a static method with the static keyword at the beginning of the method signature. All method declarations in an interface, including static methods, are implicitly public, so you can omit the public modifier.

Applications. Examples

 The Comparator interface has been enhanced with this ability with the default method reversed:

Page 25: Java 8 - Features Overview

DATE API

Page 26: Java 8 - Features Overview

26CONFIDENTIAL

Date API. Core ideas.

The new API is driven by three core ideas: 

Domain-driven design. The new API models its domain very precisely with classes that represent different use cases for Date and Time closely. This differs from previous Java libraries that were quite poor in that regard.

Immutable-value classes. One of the serious weaknesses of the existing formatters in Java is that they aren’t thread-safe. This puts the burden on developers to use them in a thread-safe manner and to think about concurrency problems in their day-to-day development of date-handling code. The new API avoids this issue by ensuring that all its core classes are immutable and represent well-defined values.

Separation of chronologies. The new API allows people to work with different calendaring systems in order to support the needs of users in some areas of the world, such as Japan or Thailand, that don’t necessarily follow ISO-8601. It does so without imposing additional burden on the majority of developers, who need to work only with the standard chronology

Page 27: Java 8 - Features Overview

27CONFIDENTIAL

Date API. Creating.

The first classes you will probably encounter when using the new API are LocalDate and LocalTime.

Standard Java getter conventions are used in order to obtain values from Java SE 8 classes

Page 28: Java 8 - Features Overview

28CONFIDENTIAL

Date API. Creating (2).

There are also methods for calculations based on the different fields. 

The new API also has the concept of an adjuster— a block of code that can be used to wrap up common processing logic

Page 29: Java 8 - Features Overview

29CONFIDENTIAL

Date API. Go ahead.

The new API supports different precision time points by offering types to represent a date, a time, and date with time, but obviously there are notions of precision that are more fine-grained than this.

Time ZonesA time zone is a set of rules, corresponding to a region in which the standard time is the same

Page 30: Java 8 - Features Overview

NASHORN. JS ENGINE

Page 31: Java 8 - Features Overview

31CONFIDENTIAL

Nashorn. JS Engine.

Nashorn's goal is to implement a lightweight high-performance JavaScript runtime in Java with a native JVM. 

MyScript.js

Page 32: Java 8 - Features Overview

32CONFIDENTIAL

?questions and answers

Page 33: Java 8 - Features Overview

33CONFIDENTIAL

http://docs.oracle.com/Follow

to get more