java 8 - under the hood
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
Java 8: Under the Hood
Vlastimil Menčík
λ - syntax
x => x + 1
_ + 1
Integer.parseInt Integer::parseInt
x -> x + 1
MyClass MyClass::new
if MyClass is a case class
λ - types
• functional interfaces aka SAM interfaces
• target typing
• applicable to legacy interfaces
• FunctionN
• String => Int
• Currying
• PartialFunction
Functional Interface
Interoperability with Scala
λ - implementation in Scala
• instances of FunctionX traits
• compiled into anonymous inner classesMyClass$$anonfun$1.class
• a lot of extra bytecode
λ - implementation in Java 8
• implementation in synthetic methods
• invokedynamic
• LambdaMetafactory
λ in bytecode
invokedynamic
• since Java 7
• java.lang.invoke
• what really gets invoked is resolved at runtime by delegating to a bootstrap method
LambdaMetafactory
• contains bootstrap methods for converting λ-expressions to functional interface objects
invokedynamic + LambdaMetafactory
invokedynamic #2
x -> x + 5
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
Java 8: Stream API
Parallel streams
• very similar to parallel collections in Scala
stream.parallel()
stream.sequential()
col.par
col.seq
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)
Default methods
public interface Iterable<T> {default void forEach(…)
}
• necessary for backwards compatibility• useful in many other cases
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
Future benefits for Scala?
• better bytecode representation for method-only traits
• implemented methods no longer copied to subtypes
• could help with binary compatibility problems
java.time
• (finally) an immutable date representation
• inspired by Joda Time
Changes in JVM
• PermGen replaced with Metaspace
• hopefully future optimizations for functional programming style
And the release date?
• umm…
• when it’s done
• hopefully next spring
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)
Q&A