Loop.times(5, () -> {
System.out.println("looping");
});
它可以有效地编译到哪一个?
for(int i = 0; i < 5; i++)
System.out.println("looping");
或类似的东西
new CallableInterfaceImpl(){
public void call(){
for(int i = 0; i < 5; i++)
System.out.println("looping");
}
}.call();
那么它会替代(某种内联),还是实际上创建一个匿名类?
VM决定如何实现lambda,而不是编译器。
请参阅《 Lambda表达式的翻译》中的Translation strategy
部分。
我们没有生成字节码来创建实现lambda表达式的对象(例如,调用内部类的构造函数),而是描述了构造lambda的方法,并将实际构造委托给语言运行时。该配方被编码在invokedynamic指令的静态和动态参数列表中。
for
就简单的编译或性能而言,从你的示例中进行构造是最有效的方法(但根据测试,性能差异很小)。
插件:
我创建并分解了两个示例:
for (String string: Arrays.asList("hello")) {
System.out.println(string);
}
反汇编的字节码,常量和其他信息:
Classfile LambdaCode.class
Last modified 30.05.2013; size 771 bytes
MD5 checksum 79bf2821b5a14485934e5cebb60c99d6
Compiled from "LambdaCode.java"
public class test.lambda.LambdaCode
SourceFile: "LambdaCode.java"
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #11.#22 // java/lang/Object."<init>":()V
#2 = Class #23 // java/lang/String
#3 = String #24 // hello
#4 = Methodref #25.#26 // java/util/Arrays.asList:([Ljava/lang/Object;)Ljava/util/List;
#5 = InterfaceMethodref #27.#28 // java/util/List.iterator:()Ljava/util/Iterator;
#6 = InterfaceMethodref #29.#30 // java/util/Iterator.hasNext:()Z
#7 = InterfaceMethodref #29.#31 // java/util/Iterator.next:()Ljava/lang/Object;
#8 = Fieldref #32.#33 // java/lang/System.out:Ljava/io/PrintStream;
#9 = Methodref #34.#35 // java/io/PrintStream.println:(Ljava/lang/String;)V
#10 = Class #36 // test/lambda/LambdaCode
#11 = Class #37 // java/lang/Object
#12 = Utf8 <init>
#13 = Utf8 ()V
#14 = Utf8 Code
#15 = Utf8 LineNumberTable
#16 = Utf8 main
#17 = Utf8 ([Ljava/lang/String;)V
#18 = Utf8 StackMapTable
#19 = Class #38 // java/util/Iterator
#20 = Utf8 SourceFile
#21 = Utf8 LambdaCode.java
#22 = NameAndType #12:#13 // "<init>":()V
#23 = Utf8 java/lang/String
#24 = Utf8 hello
#25 = Class #39 // java/util/Arrays
#26 = NameAndType #40:#41 // asList:([Ljava/lang/Object;)Ljava/util/List;
#27 = Class #42 // java/util/List
#28 = NameAndType #43:#44 // iterator:()Ljava/util/Iterator;
#29 = Class #38 // java/util/Iterator
#30 = NameAndType #45:#46 // hasNext:()Z
#31 = NameAndType #47:#48 // next:()Ljava/lang/Object;
#32 = Class #49 // java/lang/System
#33 = NameAndType #50:#51 // out:Ljava/io/PrintStream;
#34 = Class #52 // java/io/PrintStream
#35 = NameAndType #53:#54 // println:(Ljava/lang/String;)V
#36 = Utf8 test/lambda/LambdaCode
#37 = Utf8 java/lang/Object
#38 = Utf8 java/util/Iterator
#39 = Utf8 java/util/Arrays
#40 = Utf8 asList
#41 = Utf8 ([Ljava/lang/Object;)Ljava/util/List;
#42 = Utf8 java/util/List
#43 = Utf8 iterator
#44 = Utf8 ()Ljava/util/Iterator;
#45 = Utf8 hasNext
#46 = Utf8 ()Z
#47 = Utf8 next
#48 = Utf8 ()Ljava/lang/Object;
#49 = Utf8 java/lang/System
#50 = Utf8 out
#51 = Utf8 Ljava/io/PrintStream;
#52 = Utf8 java/io/PrintStream
#53 = Utf8 println
#54 = Utf8 (Ljava/lang/String;)V
{
public test.lambda.LambdaCode();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 15: 0
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=4, locals=3, args_size=1
0: iconst_1
1: anewarray #2 // class java/lang/String
4: dup
5: iconst_0
6: ldc #3 // String hello
8: aastore
9: invokestatic #4 // Method java/util/Arrays.asList:([Ljava/lang/Object;)Ljava/util/List;
12: invokeinterface #5, 1 // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
17: astore_1
18: aload_1
19: invokeinterface #6, 1 // InterfaceMethod java/util/Iterator.hasNext:()Z
24: ifeq 47
27: aload_1
28: invokeinterface #7, 1 // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
33: checkcast #2 // class java/lang/String
36: astore_2
37: getstatic #8 // Field java/lang/System.out:Ljava/io/PrintStream;
40: aload_2
41: invokevirtual #9 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
44: goto 18
47: return
LineNumberTable:
line 35: 0
line 36: 37
line 37: 44
line 38: 47
StackMapTable: number_of_entries = 2
frame_type = 252 /* append */
offset_delta = 18
locals = [ class java/util/Iterator ]
frame_type = 250 /* chop */
offset_delta = 28
}
和
Arrays.asList("hello").forEach(p -> {System.out.println(p);});
反汇编的字节码,常量和其他信息:
Classfile LambdaCode.class
Last modified 30.05.2013; size 1262 bytes
MD5 checksum 4804e0a37b73141d5791cc39d51d649c
Compiled from "LambdaCode.java"
public class test.lambda.LambdaCode
SourceFile: "LambdaCode.java"
InnerClasses:
public static final #64= #63 of #70; //Lookup=class java/lang/invoke/MethodHandles$Lookup of class java/lang/invoke/MethodHandles
BootstrapMethods:
0: #27 invokestatic java/lang/invoke/LambdaMetafactory.metaFactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
Method arguments:
#28 invokeinterface java/util/function/Consumer.accept:(Ljava/lang/Object;)V
#29 invokestatic test/lambda/LambdaCode.lambda$0:(Ljava/lang/String;)V
#30 (Ljava/lang/String;)V
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #10.#21 // java/lang/Object."<init>":()V
#2 = Class #22 // java/lang/String
#3 = String #23 // hello
#4 = Methodref #24.#25 // java/util/Arrays.asList:([Ljava/lang/Object;)Ljava/util/List;
#5 = InvokeDynamic #0:#31 // #0:lambda$:()Ljava/util/function/Consumer;
#6 = InterfaceMethodref #32.#33 // java/util/List.forEach:(Ljava/util/function/Consumer;)V
#7 = Fieldref #34.#35 // java/lang/System.out:Ljava/io/PrintStream;
#8 = Methodref #36.#37 // java/io/PrintStream.println:(Ljava/lang/String;)V
#9 = Class #38 // test/lambda/LambdaCode
#10 = Class #39 // java/lang/Object
#11 = Utf8 <init>
#12 = Utf8 ()V
#13 = Utf8 Code
#14 = Utf8 LineNumberTable
#15 = Utf8 main
#16 = Utf8 ([Ljava/lang/String;)V
#17 = Utf8 lambda$0
#18 = Utf8 (Ljava/lang/String;)V
#19 = Utf8 SourceFile
#20 = Utf8 LambdaCode.java
#21 = NameAndType #11:#12 // "<init>":()V
#22 = Utf8 java/lang/String
#23 = Utf8 hello
#24 = Class #40 // java/util/Arrays
#25 = NameAndType #41:#42 // asList:([Ljava/lang/Object;)Ljava/util/List;
#26 = Utf8 BootstrapMethods
#27 = MethodHandle #6:#43 // invokestatic java/lang/invoke/LambdaMetafactory.metaFactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
#28 = MethodHandle #9:#44 // invokeinterface java/util/function/Consumer.accept:(Ljava/lang/Object;)V
#29 = MethodHandle #6:#45 // invokestatic test/lambda/LambdaCode.lambda$0:(Ljava/lang/String;)V
#30 = MethodType #18 // (Ljava/lang/String;)V
#31 = NameAndType #46:#47 // lambda$:()Ljava/util/function/Consumer;
#32 = Class #48 // java/util/List
#33 = NameAndType #49:#50 // forEach:(Ljava/util/function/Consumer;)V
#34 = Class #51 // java/lang/System
#35 = NameAndType #52:#53 // out:Ljava/io/PrintStream;
#36 = Class #54 // java/io/PrintStream
#37 = NameAndType #55:#18 // println:(Ljava/lang/String;)V
#38 = Utf8 test/lambda/LambdaCode
#39 = Utf8 java/lang/Object
#40 = Utf8 java/util/Arrays
#41 = Utf8 asList
#42 = Utf8 ([Ljava/lang/Object;)Ljava/util/List;
#43 = Methodref #56.#57 // java/lang/invoke/LambdaMetafactory.metaFactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
#44 = InterfaceMethodref #58.#59 // java/util/function/Consumer.accept:(Ljava/lang/Object;)V
#45 = Methodref #9.#60 // test/lambda/LambdaCode.lambda$0:(Ljava/lang/String;)V
#46 = Utf8 lambda$
#47 = Utf8 ()Ljava/util/function/Consumer;
#48 = Utf8 java/util/List
#49 = Utf8 forEach
#50 = Utf8 (Ljava/util/function/Consumer;)V
#51 = Utf8 java/lang/System
#52 = Utf8 out
#53 = Utf8 Ljava/io/PrintStream;
#54 = Utf8 java/io/PrintStream
#55 = Utf8 println
#56 = Class #61 // java/lang/invoke/LambdaMetafactory
#57 = NameAndType #62:#66 // metaFactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
#58 = Class #67 // java/util/function/Consumer
#59 = NameAndType #68:#69 // accept:(Ljava/lang/Object;)V
#60 = NameAndType #17:#18 // lambda$0:(Ljava/lang/String;)V
#61 = Utf8 java/lang/invoke/LambdaMetafactory
#62 = Utf8 metaFactory
#63 = Class #71 // java/lang/invoke/MethodHandles$Lookup
#64 = Utf8 Lookup
#65 = Utf8 InnerClasses
#66 = Utf8 (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
#67 = Utf8 java/util/function/Consumer
#68 = Utf8 accept
#69 = Utf8 (Ljava/lang/Object;)V
#70 = Class #72 // java/lang/invoke/MethodHandles
#71 = Utf8 java/lang/invoke/MethodHandles$Lookup
#72 = Utf8 java/lang/invoke/MethodHandles
{
public test.lambda.LambdaCode();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 15: 0
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=4, locals=1, args_size=1
0: iconst_1
1: anewarray #2 // class java/lang/String
4: dup
5: iconst_0
6: ldc #3 // String hello
8: aastore
9: invokestatic #4 // Method java/util/Arrays.asList:([Ljava/lang/Object;)Ljava/util/List;
12: invokedynamic #5, 0 // InvokeDynamic #0:lambda$:()Ljava/util/function/Consumer;
17: invokeinterface #6, 2 // InterfaceMethod java/util/List.forEach:(Ljava/util/function/Consumer;)V
22: return
LineNumberTable:
line 28: 0
line 38: 22
}
对于Lambda示例,编译器生成的类文件更复杂且更大(771b对1262b)。
嗯,我已经试过多次了。不过,我一度认为最长的序列函数会有所帮助,因为它显示的是最长的冰雹序列。尽管如此,我似乎不知道如何查找或存储它用于查找的值。如果有人能解释一下,我将不胜感激。 我遇到的问题是我最长的启动顺序: 我不知道如何将其转换为递归,我注意到对于一些递归,我看到人们仍然在使用for循环,但我确信我们不应该使用循环。这可能是一个愚蠢的问题,但如果有人知道的话,有没有一个公式可以将循环转换为
我对编码很陌生。尝试用Java编写一个evaluatePostfix函数。我不断得到一个错误: 不兼容的类型:int不能转换为字符堆栈。push(eval(token,a,b)); 下面是我的代码块: 函数接受后缀表达式并计算结果。 这是我的eval函数:
问题内容: 在Python中,我想编写一个返回另一个函数的函数。返回的函数应该可以通过参数调用,并返回高度和半径为圆柱的体积。 我知道如何从Python中的函数返回 值 ,但是如何返回 另一个函数 ? 问题答案: 使用Python尝试一下: 这样使用它,例如与和: 注意,返回一个函数很简单,只需在函数内部定义一个新函数,然后在最后返回它- 小心地为每个函数传递适当的参数。仅供参考,从另一个函数返回
我正在测试java的lambda是否与接口函数匹配,我在下面写了代码,它可以工作。我有接口和它的。在构建时,Javalambda被转换为我的接口的实现者。 程序将打印。到目前为止还不错,但是如果我在中添加一些其他函数声明,如下所示: 线程构造函数中的lambda将无法编译,表示 那么,Java编译器什么时候可以决定lambda可以匹配MyHandler的类型,而不管我的函数名是什么?Justing
问题内容: 可能吗? 问题答案: 如果您的意思是匿名函数, 并且在Java 8之前使用的是Java版本, 那么总而言之,不是。(如果您使用Java 8+,请阅读有关lambda表达式的信息 ) 但是,您可以使用以下功能实现接口: 并且您可以将其与内部类一起使用以获取几乎匿名的功能:)
问题内容: 我试图研究应如何精确地编写异步函数。经过大量的文档研究之后,我仍然不清楚。 如何为Node编写异步函数? 如何正确实施错误事件处理? 问我问题的另一种方式是:我应该如何解释以下功能? 问题答案: 您似乎将异步IO与异步功能混淆了。由于非阻塞IO更好,所以node.js使用异步非阻塞IO。理解它的最好方法是去观看ryan dahl的一些视频。 如何为Node编写异步函数? 只需编写普通函