Sub topic 16
Under the Hood: invokedynamic and Lambda Identity
Lambdas and anonymous classes can do the same job, but Java builds them in different ways. You do not need these details to write lambdas. They do explain two rules you will see in real code: lambdas cost very little to create, and you should never test them with ==.
Two words you need
- Bytecode is the low-level code that
javacwrites into.classfiles. - The JVM (Java Virtual Machine) is the program that runs that bytecode.
How an anonymous class is compiled
When you write an anonymous class, javac creates a whole new .class file for it. If your class is called IdentityDemo, the anonymous class goes into a file named IdentityDemo$1.class. Every anonymous class adds one more file, and the JVM has to load each of those files.
How a lambda is compiled
A lambda does not get its own file. Instead, javac does two things:
- It moves the lambda's body into a hidden private method, with a name like
lambda$makeLambda$0. - At the place where the lambda appears, it writes a special bytecode instruction called invokedynamic. This instruction tells the JVM to work out, the first time this line runs, how to produce the object.
On that first run, the JVM asks a helper class called LambdaMetafactory to build a small class that implements the functional interface and calls the hidden method. After that, the line runs fast.
Lambda identity
Identity means "is this the very same object?", which is what == checks. An anonymous class expression always creates a new object. A lambda that captures no variables may return the same object every time. The Java specification allows this but does not promise it.
public class IdentityDemo {
static Runnable makeLambda() {
return () -> System.out.println("hi");
}
static Runnable makeAnon() {
return new Runnable() {
@Override
public void run() {
System.out.println("hi");
}
};
}
public static void main(String[] args) {
System.out.println(makeAnon() == makeAnon()); // false: always a new object
System.out.println(makeLambda() == makeLambda()); // often true, but not guaranteed
System.out.println(makeAnon().getClass().getName()); // IdentityDemo$1
System.out.println(makeLambda().getClass().getName()); // e.g. IdentityDemo$$Lambda/0x...
}
}Compile it, then look inside with javap, the JDK's bytecode viewer:
javac IdentityDemo.java
ls *.class
javap -c -p IdentityDemols lists IdentityDemo.class and IdentityDemo$1.class, and nothing for the lambda. In the javap output, look for invokedynamic and the private method lambda$makeLambda$0.
Side by side
| Feature | Anonymous class | Lambda |
|---|---|---|
| Extra .class file | Yes, such as `Outer$1.class` | No |
| Body stored in | Its own class | Hidden private method |
| Object creation | New object every time | May reuse one object if nothing is captured |
| Class name | Fixed, such as `Outer$1` | Generated at run time, varies |
| Meaning of `this` | The anonymous object | The enclosing object |
Exercise
You compile a class named Shop that contains two anonymous classes and three lambdas. How many .class files does javac produce, and where does the code inside each lambda end up?
Show answer
Three files: Shop.class, Shop$1.class, and Shop$2.class. The lambdas get no files of their own. Each lambda body becomes a hidden private method inside Shop.class, and the JVM builds the lambda classes at run time through invokedynamic.
Exercise
A teammate writes if (handler == otherHandler) to check whether two Runnable lambdas "do the same thing." Why is this a bug?
Show answer
== only checks whether the two references point to the same object. Two lambdas with identical code are usually different objects, and whether one lambda expression returns the same object again depends on the JVM. Lambdas have no meaningful equality, so the check tells you nothing about what they do.