New Features in JDK 8

58
New Features in JDK 8 Ivan St. Ivanov Dmitry Alexandrov Martin Toshev

description

Presentation on the new features introduced in JDK 8, presented on the 26.02.2013 in Sofia University in front of students and members of the Bulgarian java user group.

Transcript of New Features in JDK 8

Page 1: New Features in JDK 8

New Features in JDK 8

Ivan St. IvanovDmitry AlexandrovMartin Toshev

Page 2: New Features in JDK 8

Agenda

• Lambda Expressions

• Other New Features in JDK 8

• JEPs and JDK 9 Development

Page 3: New Features in JDK 8

Lambda Expressions

Page 4: New Features in JDK 8

Lambdas

• Lambdas bring anonymous function types in Java (JSR 335):

• Example:

(x,y) -> x + y

(parameters) -> {body}

Page 5: New Features in JDK 8

Lambdas

• Lambdas bring anonymous function types in Java

• Example:

… well … Groovy (a superset of java) already provides this …

(x,y) -> x + y

(parameters) -> {body}

Page 6: New Features in JDK 8

Lambdas

• Lambdas can be used in place of functional interfaces (interfaces with just one method such as Runnable)

• Example:new Thread(new Runnable() {

@Overridepublic void run() {

System.out.println("It runs !"); }}).start();

new Thread(() -> { System.out.println("It runs !"); }).start();

Page 7: New Features in JDK 8

Lambdas

• Examples of such functional interfaces:

java.lang.Runnable -> run() java.util.concurrent.Callable -> call() java.security.PrivilegedAction -> run() java.util.Comparator -> compare(T o1, T o2) java.awt.event.ActionListener ->

actionPerformed (ActionEvent e) java.lang.Iterable ->

forEach(Consumer<? super T> action)

Page 8: New Features in JDK 8

Lambdas

java.lang.Iterable -> forEach(Consumer<? super T> action) ?!?

Isn't this breaking backward compatibility ?!?

Page 9: New Features in JDK 8

Lambdas

java.lang.Iterable -> forEach(Consumer<? super T> action) ?!?

Isn't this breaking compatibility ?!?

No - because forEach is an extension method.

Page 10: New Features in JDK 8

Lambdas

Extension methods provide a mechanism for extending an existing interface without breaking backward compatibility.

public interface Iterable<T> {

default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } }}

Page 11: New Features in JDK 8

Lambdas

• This essentially means that lambdas can be used to replace all functional interfaces used in a gazillion of libraries

• Moreover lambdas increase performance when used instead of anonymous inner classes because they are implemented with the invokedynamic instruction

• In this regard many of the standard JDK class are being refactored to use lambdas

Page 12: New Features in JDK 8

Lambdas

• Additional functional interfaces are provided by the java.util.function package for use by lambdas such as:o Predicate<T> - one method with param of type T and

boolean return typeo Consumer<T> - one method with param T and no

return typeo Function<T, R> - one method with param T and return

type Ro Supplier<T> - one method with no params and return type T

Page 13: New Features in JDK 8

Stream API

• Databases and other programming languages allow us to specify aggregate operations explicitly

• The streams API provides this mechanism in the Java platform

• The notion of streams is derived from functional programming languages

Page 14: New Features in JDK 8

Stream API

• The stream API makes use of lambdas and extension methods

• Streams can be applied on collections, arrays, IO streams and generator functions

Page 15: New Features in JDK 8

Stream API

• Streams can be finite or infinite

• Streams can apply intermediate functions on the data that produce another stream (e.g. map, reduce)

Page 16: New Features in JDK 8

Stream API

java.util.stream.Stream<T> collection.stream();java.util.stream.Stream<T> collection.parallelStream();Useful methods:

o filter(Predicate), map(Function), reduce(BinaryOperator), collect()

o sum(), min(), max(), count()o anyMatch(), allMatch(), forEach(),

Page 17: New Features in JDK 8

Stream internals

• Stream operations are composed into a pipeline

• Streams are lazy: computation is performed when the terminal operation is invoked

int sum = widgets.stream() .filter(w -> w.getColor() == RED) .mapToInt(w -> w.getWeight()) .sum();

Page 18: New Features in JDK 8

Method references

• Intended to be used in lambda expressions, preventing unnecessary boilerplate

• Example:

• Lambda parameter list and return type must match the signature of the method

books.stream().map(b -> b.getTitle()) books.stream().map(Book::getTitle)

Page 19: New Features in JDK 8

Method references – static methods

public class Printers { public static void print(String s) {...} }

Arrays.asList("a", "b", "c").forEach(Printers::print)

Page 20: New Features in JDK 8

Method references – instance methods (1)

public class Document { public String getPageContent(int pageNumber) { return this.pages.get(pageNumber).getContent(); }}

public static void printPages(Document doc, int[] pageNumbers) { Arrays.stream(pageNumbers) .map(doc::getPageContent) .forEach(Printers::print); }

Page 21: New Features in JDK 8

Method references – instance methods (2)

public class Document { public String getPageContent(int pageNumber) { return this.pages.get(pageNumber).getContent(); }}

public static void printDocuments(List<Page> pages) { pages.stream() .map(Page::getContent) .forEach(Printers::print); }

Page 22: New Features in JDK 8

Method references – constructors

public static Stream<Page> createPagesFrom(Stream<String> contents) { return contents.map(Page::new). }

Page 23: New Features in JDK 8

Lambda Expressions:Hands-On-Lab

Page 24: New Features in JDK 8

Lambdas Hands-on-Lab:Overview

• Environment setup

• Concept

• Task categories

Page 25: New Features in JDK 8

Lambdas Hands-on-Lab:Environment Setup

• Download and install OpenJDK 8 early access

build from https://jdk8.java.net/lambda/ (… or build yours from the OpenJDK repo)

Page 26: New Features in JDK 8

Lambdas Hands-on-Lab:Environment Setup

• Download and install an IDE:

o Eclipsehttp://www.oracle.com/technetwork/articles/java/lambda-1984522.html(for Eclipse 4.3: Start IDE -> Install New Software)http://build.eclipse.org/eclipse/builds/4P/siteDir/updates/4.3-P-builds

o IntelliJ IDEA CEhttp://www.jetbrains.com/idea/free_java_ide.html

Page 27: New Features in JDK 8

Lambdas Hands-on-Lab:Environment Setup

• Get the sources:

or download

• Import the sources as Maven projects in the IDE• Set the Java 8 profile (source compatibility level)• Check if the sources compile and that you are able to run

ConfigureYourLambdaBuildOfJdk

git clone https://github.com/AdoptOpenJDK/lambda-tutorial

http://dmitryalexandrov.net/lambda.zip

Page 28: New Features in JDK 8

Lambdas Hands-on-Lab:Concept

• We have several classes and tests for them

• Find “// your code here” and write the code !

• Run the tests. Try to make them green!(… read the comments.. They are useful !)

Page 29: New Features in JDK 8

Lambdas Hands-on-Lab:Task Categories

• Internal vs External Iteration

• Filtering and Collecting

• Mapping

• Method References

Page 30: New Features in JDK 8

Lambdas Hands-on-Lab:Internal vs External Iteration

Java 8 (Internal)

myShapes.forEach(shape -> shape.setColor(RED));

Java 7 (External)

for (Shape shape: myShapes) { shape.setColor(RED);

}

Page 31: New Features in JDK 8

Lambdas Hands-on-Lab:Filtering and Collecting

Java8otherThings.stream()

.filter(s -> satisfiesSomeCondition(s))

.collect(Collectors.toList());

Java7for (String s: otherThings) {

if (satisfiesSomeCondition(s)) { things.add(s);

}}

Page 32: New Features in JDK 8

Lambdas Hands-on-Lab:Mapping

Java8mixedCaseStrings.stream()

.map(s -> s.toUpperCase()) .collect(Collectors.toList());

Java7List<String> upperCaseStrings = new ArrayList<>();for (String s: mixedCaseStrings) {

upperCaseStrings.add(s.toUpperCase());}

Page 33: New Features in JDK 8

Lambdas Hands-on-Lab:Mapping

Using a lambda expression:

myStrings.map(s -> s.toUpperCase());

Using a method reference:

myStrings.map(String::toUpperCase);

Can be used with:• Static methods belonging to a particular class (<class>::<staticMethod>)• Instance methods bound to a particular object instance (<obj> ::

<instanceMethod>)• Instance methods bound to a particular class (<class> :: <instanceMethod>)• Constructor belonging to a particular class (<class> :: new)

Page 34: New Features in JDK 8

Lambdas Hands-on-Lab:Default Methods

Java 8

public interface A { default void foo(){ System.out.println("Calling A.foo()"); }}

public class Clazz implements A {}

Clazz clazz = new Clazz();clazz.foo(); // Calling A.foo()

Page 35: New Features in JDK 8

Lambdas Hands-on-Lab:Default Methods

Java 8

public class Clazz2 implements A { @Override public void foo(){ System.out.println("Calling A.foo()"); }}

Clazz2 clazz2 = new Clazz2();clazz2.foo(); // Calling A.foo()

Page 36: New Features in JDK 8

Lambdas Hands-on-Lab:Let's have some fun

Page 37: New Features in JDK 8

Lambdas Hands-on-Lab:Hints

Examples:1. (int x, int y) -> x + y2. (x, y) -> x - y3. () -> 42 4. (String s) -> System.out.println(s)5. x -> 2 * x6. c -> { int s = c.size(); c.clear(); return s; }7. myShapes.forEach(shape -> shape.setColor(RED));8. otherThings.stream().filter(s -> satisfiesSomeCondition(s)).collect(Collectors.toList());9. mixedCaseStrings.stream().map(s -> s.toUpperCase()).collect(Collectors.toList());10. myStrings.map(String::toUpperCase); 11. public interface A { default void foo(){ System.out.println("Calling A.foo()");} }

The basic syntax of a lambda is either (parameters) -> expressionor (parameters) -> { statements; }

Page 38: New Features in JDK 8

Other New Features in JDK 8

Page 39: New Features in JDK 8

Date and Time API (JSR 310)

• Provides a new date, time and calendar API for the Java SE platform

• Examples:Clock clock = Clock.systemUTC();ZoneId zone = ZoneId.systemDefault();ZoneId customZone = ZoneId.of("Europe/Berlin");Clock customClock = Clock.system(customZone);DateTimeFormatter formatter = DateTimeFormatter.

ofPattern("MM-DD-YYYY");LocalDate date = LocalDate.now();String formattedDate = date.format(formatter);System.out.println(formattedDate);

Page 40: New Features in JDK 8

Nashorn (via JSR 223)

• Nashorn is a JavaScript engine developed in the Java language

• A command line tool (jjs) for running javascript without using Java is also provided

Page 41: New Features in JDK 8

Nashorn (via JSR 223)

• Nashorn is a JavaScript engine developed in the Java language

• A command line tool (jjs) for running javascript without using Java is also provided

• … well … JDK 6 is already shipped with Mozilla's Rhino JavaScript engine but is slower …

Page 42: New Features in JDK 8

Nashorn (via JSR 223)

• Example (calling JavaScript from Java):

ScriptEngineManager m = new ScriptEngineManager();ScriptEngine nashorn = m.getEngineByName("nashorn");try {

nashorn.eval("print('Hello, world')");} catch (ScriptException e) {}

Page 43: New Features in JDK 8

Nashorn (via JSR 223)

• Example (calling Java from JavaScript):

var ExampleType = Java.type("com.sample.Example");var example = new ExampleType("arg");print(example.method())

Page 44: New Features in JDK 8

Parameter Names (JEP 118)

• Parameter names for method/constructor parameters can be retrieved at runtime via reflection

Method method = …Parameter param = method.getParameters()[0];System.out.println(param.getName());

Page 45: New Features in JDK 8

Annotation on Java Types (JSR 308, JEP 104)

• Extend the set of annotatable locations in the syntax of the Java programming language

• Allow type checkers in the form of compiler plug-ins to provide stronger type checking

• Sample annotations: @notnull, @readonly

• Enhancements to JSR 269 (Pluggable Annotation Processing API)

Page 46: New Features in JDK 8

Repeating Annotations(JEP 120)

• Change the Java programming language to allow multiple annotations with the same type on a single program element

Page 47: New Features in JDK 8

Base64 Encoding/Decoding(JEP 135)

• Defines a standard API for Base64 encoding and decoding:java.util.Base64.Encoderjava.util.Base64.Decoder

Page 48: New Features in JDK 8

Concurrency Updates(JEP 155)

• Scalable updatable variables (for frequent updates from multiple threads) are part of the concurrency updates:java.util.concurrent.atomic.DoubleAccumulatorjava.util.concurrent.atomic.DoubleAdderjava.util.concurrent.atomic.LongAccumulatorjava.util.concurrent.atomic.LongAdder

Page 49: New Features in JDK 8

Compact Profiles(JEP 161)

• Defines a few subset Profiles of the Java SE Platform Specification so that applications that do not require the entire Platform can be deployed and run on small devices.

• Currently three profiles: o compact1o compact2o compact3

Page 50: New Features in JDK 8

Other

• Prepare for modularization (JEP 162) - changes and deprecation of APIs to prepare for the modularization of the platform as undertaken by project Jigsaw

• Permanent generation space removed - objects from this space such as class metadata, static variables and interned strings moved to heap space

(improved convergence between OpenJDK and Oracle's JRockit)

Page 51: New Features in JDK 8

Some books

Page 52: New Features in JDK 8

JEPs and JDK 9 Development

Page 53: New Features in JDK 8

JEPs and JDK 9 Development

So lets see what JEPs are left for JDK 9 ->http://openjdk.java.net/jeps/

Page 54: New Features in JDK 8

Q&A

Page 55: New Features in JDK 8

References

OpenJDK 8 early access buildshttps://jdk8.java.net/

Oracle JDK 8 early access buildshttp://www.oracle.com/technetwork/java/javase/downloads/ea-jsp-142245.html

Eclipse with JDK 8 supporthttps://wiki.eclipse.org/JDT_Core/Java8

Page 56: New Features in JDK 8

References

OpenJDK mercurial repositorieshttp://hg.openjdk.java.net/

OpenJDK 9 build instructionshttp://hg.openjdk.java.net/jdk9/jdk9/raw-file/tip/README-builds.html

JSR 337: Java SE 8 (Early Draft Review Specification)http://cr.openjdk.java.net/~mr/se/8/java-se-8-edr-spec.html

Page 57: New Features in JDK 8

References

Lambda tutorialhttps://github.com/AdoptOpenJDK/lambda-tutorial

State of the Lambdahttp://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-final.html

Lambda FAQhttp://www.lambdafaq.org/