Java 8 - Under the Hood


Published on

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.

Published in: Technology, Education

Java 8 - Under the Hood

  1. 1. Java 8: Under the HoodVlastimil Menčík
  2. 2. λ - syntaxx => x + 1_ + 1Integer.parseInt Integer::parseIntx -> x + 1MyClass MyClass::newif MyClass is a case class
  3. 3. λ - types• functional interfacesaka SAM interfaces• target typing• applicable to legacyinterfaces• FunctionN• String => Int• Currying• PartialFunction
  4. 4. Functional Interface
  5. 5. Interoperability with Scala
  6. 6. λ - implementation in Scala• instances of FunctionX traits• compiled into anonymous inner classesMyClass$$anonfun$1.class• a lot of extra bytecode
  7. 7. λ - implementation in Java 8• implementation in synthetic methods• invokedynamic• LambdaMetafactory
  8. 8. λ in bytecode
  9. 9. invokedynamic• since Java 7• java.lang.invoke• what really gets invoked is resolved at runtimeby delegating to a bootstrap method
  10. 10. LambdaMetafactory• contains bootstrap methods for converting λ-expressions to functional interface objects
  11. 11. invokedynamic + LambdaMetafactoryinvokedynamic #2
  12. 12. x -> x + 5
  13. 13. 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
  14. 14. Java 8: Stream API
  15. 15. Parallel streams• very similar to parallel collections in Scalastream.parallel()stream.sequential()col.parcol.seq
  16. 16. 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)
  17. 17. Default methodspublic interface Iterable<T> {default void forEach(…)}• necessary for backwards compatibility• useful in many other cases
  18. 18. Does Java now have traits?• sort of, but not really – no state, no self types, no linearization (possiblediamond inheritance issues), …• default methods are means of painless APIevolution• traits are means of modularization
  19. 19. Future benefits for Scala?• better bytecode representation for method-only traits• implemented methods no longer copied tosubtypes• could help with binary compatibility problems
  20. 20. java.time• (finally) an immutable date representation• inspired by Joda Time
  21. 21. Changes in JVM• PermGen replaced with Metaspace• hopefully future optimizations for functionalprogramming style
  22. 22. And the release date?• umm…• when it’s done• hopefully next spring
  23. 23. Where to go next?••••– my more Java-focused talk (in Czech)
  24. 24. Q&A
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.