位于Java类文件中的lambda的代码在哪里? [英] Where's the code for a lambda located in a java class file?

查看:69
本文介绍了位于Java类文件中的lambda的代码在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆