Java 8 - Under the Hood

24
Java 8: Under the Hood Vlastimil Menčík

description

Presentation about upcoming features of Java 8 mainly with regard to functional programming (lambdas). The presentation compares the features with Scala. It was presented by Vlastimil Menčík at the May's Czech Scala Enthusiasts meetup held at Faculty of Information Technology in Prague.

Transcript of Java 8 - Under the Hood

Page 1: Java 8 - Under the Hood

Java 8: Under the Hood

Vlastimil Menčík

Page 2: Java 8 - Under the Hood

λ - syntax

x => x + 1

_ + 1

Integer.parseInt Integer::parseInt

x -> x + 1

MyClass MyClass::new

if MyClass is a case class

Page 3: Java 8 - Under the Hood

λ - types

• functional interfaces aka SAM interfaces

• target typing

• applicable to legacy interfaces

• FunctionN

• String => Int

• Currying

• PartialFunction

Page 4: Java 8 - Under the Hood

Functional Interface

Page 5: Java 8 - Under the Hood

Interoperability with Scala

Page 6: Java 8 - Under the Hood

λ - implementation in Scala

• instances of FunctionX traits

• compiled into anonymous inner classesMyClass$$anonfun$1.class

• a lot of extra bytecode

Page 7: Java 8 - Under the Hood

λ - implementation in Java 8

• implementation in synthetic methods

• invokedynamic

• LambdaMetafactory

Page 8: Java 8 - Under the Hood

λ in bytecode

Page 9: Java 8 - Under the Hood

invokedynamic

• since Java 7

• java.lang.invoke

• what really gets invoked is resolved at runtime by delegating to a bootstrap method

Page 10: Java 8 - Under the Hood

LambdaMetafactory

• contains bootstrap methods for converting λ-expressions to functional interface objects

Page 11: Java 8 - Under the Hood

invokedynamic + LambdaMetafactory

invokedynamic #2

Page 12: Java 8 - Under the Hood

x -> x + 5

Page 13: Java 8 - Under the Hood

When will this be in Scala?

• after Scala moves away from Java 6

• 2.11 will have experimental Java 7 backend

• 2.12 will target Java 8

Page 14: Java 8 - Under the Hood

Java 8: Stream API

Page 15: Java 8 - Under the Hood

Parallel streams

• very similar to parallel collections in Scala

stream.parallel()

stream.sequential()

col.par

col.seq

Page 16: Java 8 - Under the Hood

More lambdas in Java API

• java.util.function– “standard” functional interfaces– Function, BiFunction, Predicate, Supplier,

• java.util.Optional<T>

public T orElseGet(Supplier<? extends T> other)

Page 17: Java 8 - Under the Hood

Default methods

public interface Iterable<T> {default void forEach(…)

}

• necessary for backwards compatibility• useful in many other cases

Page 18: Java 8 - Under the Hood

Does Java now have traits?

• sort of, but not really – no state, no self types, no linearization (possible

diamond inheritance issues), …

• default methods are means of painless API evolution

• traits are means of modularization

Page 19: Java 8 - Under the Hood

Future benefits for Scala?

• better bytecode representation for method-only traits

• implemented methods no longer copied to subtypes

• could help with binary compatibility problems

Page 20: Java 8 - Under the Hood

java.time

• (finally) an immutable date representation

• inspired by Joda Time

Page 21: Java 8 - Under the Hood

Changes in JVM

• PermGen replaced with Metaspace

• hopefully future optimizations for functional programming style

Page 22: Java 8 - Under the Hood

And the release date?

• umm…

• when it’s done

• hopefully next spring

Page 23: Java 8 - Under the Hood

Where to go next?

• https://jdk8.java.net/lambda/

• http://www.lambdafaq.org/

• http://lambdadoc.net/api/

• http://youtu.be/-7OB2PxB_zg– my more Java-focused talk (in Czech)

Page 24: Java 8 - Under the Hood

Q&A