问题描述
我正在尝试在 java 中使用 lambda 表达式创建一个示例,并且我使用的是官方 JDK8.我的示例运行成功.但是当我试图检查编译器如何将 lambda 表达式转换为字节码时,这让我有些困惑.以下是我的示例代码:-
I am trying to create an example using lambda expression in java and i am using offical JDK8. My example was run successfully. But when i trying to check how the compiler translate lambda expression into byte code, this makes me some confusion.Following is the code of my example:-
public class LambdaTest {
public Integer lambdaBinaryOpertor(BinaryOperator<Integer> binaryOperator) {
return binaryOperator.apply(60, 72);
}
public static void main(String[] args) {
LambdaTest test = new LambdaTest();
BinaryOperator<Integer> binaryOperator = (a, b) -> a*b;
System.out.println("Additon using Lambda BinaryOperator: "+test.lambdaBinaryOpertor(binaryOperator));
}
}
在这篇文章中,他们讨论了编译器如何将 lambda 表达式转换为字节码.根据该文档,lambda 表达式转换为 static
方法和 lambda 表达式声明的位置,有 lambda static
方法的引用.以下示例在文章中:
In this Article, they discuss about how compiler translate the lambda expressions into byte code. According to this document the lambda expression convert into static
method and the location where lambda expression declare, have reference of lambda static
method. The following example is in the article :
//Source code
class A {
public void foo() {
List<String> list = ...
list.forEach( s -> { System.out.println(s); } );
}
}
//After compile above code "translate code "
class A {
public void foo() {
List<String> list = ...
list.forEach( [lambda for lambda$1 as Block] );
}
static void lambda$1(String s) {
System.out.println(s);
}
}
我的示例运行良好,并为我们提供了适当的结果.但是当我尝试运行 javap
命令来检查类的字节码时,字节码中没有用于 lambdas 的静态方法.
My example run fine and give us the appropriate result. But when i trying to run the javap
command for check the byte code of class, there is no static method for lambdas in byte code.
c:>javap LambdaTest
Compiled from "LambdaTest.java"
public class LambdaTest {
public LambdaTest();
public java.lang.Integer lambdaBinaryOpertor(java.util.function.BinaryOperator <java.lang.Integer>);
public static void main(java.lang.String[]);
}
在泛型的情况下,桥接方法是由编译器创建的,我们也将使用 javap
命令检查此方法,但在 lambdas 的情况下,没有 static
方法.文章发表于 2012 年,java 8 于 2014 年 3 月发布.所以我对 lambda 的跨国有一些疑问:
In the case of generics, bridge method is created by compiler and we will also check this method using javap
command, but in the case of lambdas there is no static
method. The article was published at 2012 and java 8 was launch at march 2014. So i have some queries regarding transnational of lambda:
- 在本文发布之后,JDK 8 中是否有任何用于 lambda 的新实现,否则我会在检查 lambda 方法时做错什么?
- 编译器如何处理 lambda 表达式?
- JVM如何调用lambda表达式?
推荐答案
使用 javap 附加参数来打印关于类的完整信息:javap -v -p -s -c
Use javap additional arguments to print full information about class: javap -v -p -s -c
对于您的示例,lambda 的源代码将是:
For your example, source code of lambda will be:
private static java.lang.Integer lambda$main$0(java.lang.Integer, java.lang.Integer);
descriptor: (Ljava/lang/Integer;Ljava/lang/Integer;)Ljava/lang/Integer;
flags: ACC_PRIVATE, ACC_STATIC, ACC_SYNTHETIC
Code:
stack=2, locals=2, args_size=2
0: aload_0
1: invokevirtual #17 // Method java/lang/Integer.intValue:()I
4: aload_1
5: invokevirtual #17 // Method java/lang/Integer.intValue:()I
8: imul
9: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
12: areturn
LineNumberTable:
line 10: 0
LocalVariableTable:
Start Length Slot Name Signature
0 13 0 a Ljava/lang/Integer;
0 13 1 b Ljava/lang/Integer;
}
这篇关于Lambda 表达式如何在 Java 字节码中翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!