Lambdas in Java: A Peek Under the Hood • Brian Goetz • GOTO 2013

GOTO Conferences · 39K views · 2014 · 53:05

Open on YouTube

Programming language designCompilers, interpreters & program analysisFunctional programmingtalk

Why Stack Overflow recommends it

Stack Overflow users link it when answering whether Java lambdas are just anonymous inner classes, since it shows they compile to invokedynamic and method handles instead.

Recommended by 6 people across 6 different months, 2014 to 2020.

What it is

A conference talk by Brian Goetz, Java Language Architect at Oracle, explaining how Java 8 lambda expressions are implemented. It covers functional interfaces, method handles, the invokedynamic bytecode, translation strategies, performance costs and serialization.

What people said

Strong rec PeterK · 2014-10 · Stack Overflow · on “Are Java 8 lambdas compiled as inner classes, methods or something else?”

I asked myself the same question and found this video [this video] , a talk by Brian Goetz. It is very useful introduction to how lambdas are implemented in java. Edit (summary): Watched it a while back, so this might not be completely correct. When the files are compiled, the compiler leaves a description of what the lambda should do. The JRE, then when running the code, will then decide how to implement the lambda. There are several options, inline, method reference, anonymous class. It will then use dynamic allocation to allocate the implementation.

Strong rec Sleiman Jneidi · 2015-05 · Stack Overflow · on “How are lazy streams implemented in Java 8?”

No reflection or proxies. Reflection and proxies come with a performance cost that should be avoided unless there isn't an alternative and performance is number one in Java . What makes laziness possible is the functional style of doing things. Basically a stream starts with a source(ex: List), number of intermediate operations (ex: filters, map..) , and a terminal operation (ex: count, sum, etc..). Intermediate steps execute lazily because you pass functions (lambdas) that get chained in the pipeline to be executed at the terminal step. ex: filter(Predicate<? super T>) filter in this example expects a function that tells us whether an object in the stream meets some criteria or not. A lot o

Strong rec M. Prokhorov · 2017-05 · Stack Overflow · on “Caching using Supplier Java 8”

@VitalyBelkevich, check out this very good video [this video] about how lambdas work and why, it helped me a lot when I was learning it.

Strong rec harel · 2017-09 · Stack Overflow · on “Spark Java API Task not serializable when not using Lambda”

I was under the false impression that Lambdas are implemented under the hood as inner classes. This is no longer the case (very helpful talk [this video] ). Also, as T. Gawęda answered, inner classes do in fact hold reference to the outer class, even if it is not needed ( here [LINK] ). This difference explains the behavior.

Recommends Brian Goetz · 2014-09 · Stack Overflow · on “How to correctly determine that an object is a lambda?”

@damluar Too many reasons to fit in 600 characters! This is a classic example of one of those ideas that is obvious and yet in hindsight turned out to be the wrong thing to do. See [this video] for some of the reasons.

Recommends Vince · 2015-07 · Stack Overflow · on “How to use ActionListener, and pass stuff in and out?”

@maskacovnik They are not "syntactic sugar" for anonymous classes. You can tell by the lack of a $.class file that anonymous classes generate. A lambda expression uses a Method Handle [LINK] rather than defining a new class and instantiating it. If you view the bytecode, you'll notice that it's very different. I suggest watching Brain Goetz's "Lambdas, A Peek Under the Hood" [this video] .

Cites Ali Dehghani · 2020-04 · Stack Overflow · on “What's invokedynamic and how do I use it?”

As part of my Java Records [LINK] article, I articulated about the motivation behind Invoke Dynamic. Let's start with a rough definition of Indy. Introducing Indy Invoke Dynamic (Also known as Indy ) was part of JSR 292 [LINK] intending to enhance the JVM support for Dynamic Type Languages. After its first release in Java 7, The invokedynamic opcode along with its java.lang.invoke luggage is used quite extensively by dynamic JVM-based languages like JRuby. Although indy specifically designed to enhance the dynamic language support, it offers much more than that. As a matter of fact, it’s suitable to use wherever a language designer needs any form of dynamicity, from dynamic type acrobatics t