java lambda表达式编译_什么是Java 8 Lambda表达式编译?

参见英文答案 >

How will Java lambda functions be compiled?

考虑下面的Java 8代码段。

public static void main(String[] args) {

List integers = Arrays.asList(1, 2, 3, 4, 5);

Consumer consumer = x -> System.out.print(x);

integers.forEach(consumer);

}

什么是消费者消费者= x – > System.out.print(x)被编译成?

我明白Lambdas没有被实现为匿名内部类。然而消费者是一个界面,因此x – > System.out.print(x)必须生成某种对象,但是不清楚正在生成什么样的对象。

Java 8中是否有一些新类型的对象来表示lambda表达式?

更新这里是反编译程序,该程序被遵循的eclipse java 8编译器,下面的输出是从eclipse当你打开一个类文件。

看起来lambda表达式变成一个静态方法,该类包含lambda表达式private static synthetic void lambda $ 0(java.lang.Integer x);

// Compiled from Example.java (version 1.8 : 52.0, super bit)

public class Example {

// Method descriptor #6 ()V

// Stack: 1, Locals: 1

public Example();

0 aload_0 [this]

1 invokespecial java.lang.Object() [8]

4 return

Line numbers:

[pc: 0, line: 7]

Local variable table:

[pc: 0, pc: 5] local: this index: 0 type: Example

// Method descriptor #15 ([Ljava/lang/String;)V

// Stack: 4, Locals: 3

public static void main(java.lang.String[] args);

0 iconst_5

1 anewarray java.lang.Integer [16]

4 dup

5 iconst_0

6 iconst_1

7 invokestatic java.lang.Integer.valueOf(int) : java.lang.Integer [18]

10 aastore

11 dup

12 iconst_1

13 iconst_2

14 invokestatic java.lang.Integer.valueOf(int) : java.lang.Integer [18]

17 aastore

18 dup

19 iconst_2

20 iconst_3

21 invokestatic java.lang.Integer.valueOf(int) : java.lang.Integer [18]

24 aastore

25 dup

26 iconst_3

27 iconst_4

28 invokestatic java.lang.Integer.valueOf(int) : java.lang.Integer [18]

31 aastore

32 dup

33 iconst_4

34 iconst_5

35 invokestatic java.lang.Integer.valueOf(int) : java.lang.Integer [18]

38 aastore

39 invokestatic java.util.Arrays.asList(java.lang.Object[]) : java.util.List [22]

42 astore_1 [integers]

43 invokedynamic 0 accept() : java.util.function.Consumer [31]

48 astore_2 [consumer]

49 getstatic java.lang.System.out : java.io.PrintStream [32]

52 aload_2 [consumer]

53 invokevirtual java.lang.Object.getClass() : java.lang.Class [38]

56 invokevirtual java.lang.Class.getCanonicalName() : java.lang.String [42]

59 invokevirtual java.io.PrintStream.println(java.lang.String) : void [48]

62 getstatic java.lang.System.out : java.io.PrintStream [32]

65 aload_2 [consumer]

66 invokevirtual java.lang.Object.getClass() : java.lang.Class [38]

69 invokevirtual java.lang.Class.getTypeName() : java.lang.String [54]

72 invokevirtual java.io.PrintStream.println(java.lang.String) : void [48]

75 aload_1 [integers]

76 aload_2 [consumer]

77 invokeinterface java.util.List.forEach(java.util.function.Consumer) : void [57] [nargs: 2]

82 return

Line numbers:

[pc: 0, line: 10]

[pc: 43, line: 12]

[pc: 49, line: 14]

[pc: 62, line: 15]

[pc: 75, line: 17]

[pc: 82, line: 18]

Local variable table:

[pc: 0, pc: 83] local: args index: 0 type: java.lang.String[]

[pc: 43, pc: 83] local: integers index: 1 type: java.util.List

[pc: 49, pc: 83] local: consumer index: 2 type: java.util.function.Consumer

Local variable type table:

[pc: 43, pc: 83] local: integers index: 1 type: java.util.List

[pc: 49, pc: 83] local: consumer index: 2 type: java.util.function.Consumer

// Method descriptor #73 (Ljava/lang/Integer;)V

// Stack: 2, Locals: 1

private static synthetic void lambda$0(java.lang.Integer x);

0 getstatic java.lang.System.out : java.io.PrintStream [32]

3 aload_0 [x]

4 invokevirtual java.io.PrintStream.print(java.lang.Object) : void [74]

7 return

Line numbers:

[pc: 0, line: 12]

Local variable table:

[pc: 0, pc: 8] local: x index: 0 type: java.lang.Integer

Inner classes:

[inner class info: #96 java/lang/invoke/MethodHandles$Lookup, outer class info: #98 java/lang/invoke/MethodHandles

inner name: #100 Lookup, accessflags: 25 public static final]

Bootstrap methods:

0 : # 89 arguments: {#90,#93,#94}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值