Java Reference
In-Depth Information
class InnerClass$1 implements
java.util.function.Function<java.lang.Object, java.lang.String> {
final InnerClass this$0;
public java.lang.String apply(java.lang.Object);
Code:
0: aload_1
1: invokevirtual #3 //Method
java/lang/Object.toString:()Ljava/lang/String;
4: areturn
}
D.3. InvokeDynamic to the rescue
Now let's try to do the same using the new Java 8 syntax as a lambda expression. Inspect the
generated class file of the code in the following listing.
Listing D.2. A Function implemented with a lambda expression
import java.util.function.Function;
public class Lambda {
Function<Object, String> f = obj -> obj.toString();
}
You'll find the following bytecode instructions:
0: aload_0
1: invokespecial #1
// Method java/lang/Object."<init>":()V
4: aload_0
5: invokedynamic #2, 0 // InvokeDynamic
#0:apply:()Ljava/util/function/Function;
10: putfield
#3
// Field f:Ljava/util/function/Function;
13: return
We explained the drawbacks in translating a lambda expression in an anonymous inner class,
and indeed you can see that the result is very different. The creation of an extra class has been
replaced with an invokedynamic instruction.
 
Search WWH ::




Custom Search