Download - Java 8 - Under the Hood
![Page 1: Java 8 - Under the Hood](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/1.jpg)
Java 8: Under the Hood
Vlastimil Menčík
![Page 2: Java 8 - Under the Hood](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/2.jpg)
λ - 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](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/3.jpg)
λ - types
• functional interfaces aka SAM interfaces
• target typing
• applicable to legacy interfaces
• FunctionN
• String => Int
• Currying
• PartialFunction
![Page 4: Java 8 - Under the Hood](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/4.jpg)
Functional Interface
![Page 5: Java 8 - Under the Hood](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/5.jpg)
Interoperability with Scala
![Page 6: Java 8 - Under the Hood](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/6.jpg)
λ - 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](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/7.jpg)
λ - implementation in Java 8
• implementation in synthetic methods
• invokedynamic
• LambdaMetafactory
![Page 8: Java 8 - Under the Hood](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/8.jpg)
λ in bytecode
![Page 9: Java 8 - Under the Hood](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/10.jpg)
LambdaMetafactory
• contains bootstrap methods for converting λ-expressions to functional interface objects
![Page 11: Java 8 - Under the Hood](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/11.jpg)
invokedynamic + LambdaMetafactory
invokedynamic #2
![Page 12: Java 8 - Under the Hood](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/12.jpg)
x -> x + 5
![Page 13: Java 8 - Under the Hood](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/14.jpg)
Java 8: Stream API
![Page 15: Java 8 - Under the Hood](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/15.jpg)
Parallel streams
• very similar to parallel collections in Scala
stream.parallel()
stream.sequential()
col.par
col.seq
![Page 16: Java 8 - Under the Hood](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/20.jpg)
java.time
• (finally) an immutable date representation
• inspired by Joda Time
![Page 21: Java 8 - Under the Hood](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/21.jpg)
Changes in JVM
• PermGen replaced with Metaspace
• hopefully future optimizations for functional programming style
![Page 22: Java 8 - Under the Hood](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/22.jpg)
And the release date?
• umm…
• when it’s done
• hopefully next spring
![Page 23: Java 8 - Under the Hood](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062303/554f5db6b4c905c8088b4808/html5/thumbnails/24.jpg)
Q&A