位于Java类文件中的lambda的代码在哪里? [英] Where's the code for a lambda located in a java class file?
问题描述
我有这个Java源文件:
I've this java source file:
import java.util.function.*;
public class t {
public static void main(String[] args) {
Function<Integer,Integer> r = (a) -> a*a+2*a+1;
System.out.println(r.apply(2));
}
}
我编译了它,并且按预期运行. 这里是javap -c -v t
的输出,我找不到lambda的位置.告诉代码,每当调用lambda时,告诉jvm使用输入Integer
计算表达式的字节码在哪里?
I compile it and it works as expected. Here's the output of javap -c -v t
, and I can't find the location of lambda in it. Where's the bytecode which tells the jvm to compute the expression with the input Integer
whenever the lambda is envoked?
推荐答案
如果您想查看Lambda正文a*a+2*a+1
的代码,则应调用javap -c -v -p t
来查看 private 方法:
If you want to see the code of your lambda body a*a+2*a+1
, you should call javap -c -v -p t
to see also the private methods:
private static java.lang.Integer lambda$main$0(java.lang.Integer);
descriptor: (Ljava/lang/Integer;)Ljava/lang/Integer;
flags: ACC_PRIVATE, ACC_STATIC, ACC_SYNTHETIC
Code:
stack=3, locals=1, args_size=1
0: aload_0
1: invokevirtual #7 // Method java/lang/Integer.intValue:()I
4: aload_0
5: invokevirtual #7 // Method java/lang/Integer.intValue:()I
8: imul
9: iconst_2
10: aload_0
11: invokevirtual #7 // Method java/lang/Integer.intValue:()I
14: imul
15: iadd
16: iconst_1
17: iadd
18: invokestatic #4 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
21: areturn
LineNumberTable:
line 4: 0
有关lambda内部实现的更详细的答案在这里:如何编译Java lambda函数?
More detailed answer about the lambda inner implementation is here: How will Java lambda functions be compiled?
这篇关于位于Java类文件中的lambda的代码在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!