What's byecode of lambda be generated in Java is interesting for me. From below simple code, we can execute javap to get the compiled asm code which will be performed by JVM.
{
private Function<String, Integer> f = s -> Integer.parseInt(s);
public String test()
{
Function<String, String> c = (a) -> a.toUpperCase();
Supplier<Date> s = () -> new Date();
return c.apply("java") + s.get() + f.apply("99");
}
}
javap -c -p LambdaBytecode.class
public class org.javasafari.lambda.LambdaBytecode {
private java.util.function.Function<java.lang.String, java.lang.Integer> f;
public org.javasafari.lambda.LambdaBytecode();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: invokedynamic #2, 0 // InvokeDynamic #0:apply:()Ljava/util/function/Function;
10: putfield #3 // Field f:Ljava/util/function/Function;
13: return
public java.lang.String test();
Code:
0: invokedynamic #4, 0 // InvokeDynamic #1:apply:()Ljava/util/function/Function;
5: astore_1
6: invokedynamic #5, 0 // InvokeDynamic #2:get:()Ljava/util/function/Supplier;
11: astore_2
12: new #6 // class java/lang/StringBuilder
15: dup
16: invokespecial #7 // Method java/lang/StringBuilder."<init>":()V
19: aload_1
20: ldc #8 // String java
22: invokeinterface #9, 2 // InterfaceMethod java/util/function/Function.apply:(Ljava/lang/Object;)Ljava/lang/Object;
27: checkcast #10 // class java/lang/String
30: invokevirtual #11 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
33: aload_2
34: invokeinterface #12, 1 // InterfaceMethod java/util/function/Supplier.get:()Ljava/lang/Object;
39: invokevirtual #13 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
42: aload_0
43: getfield #3 // Field f:Ljava/util/function/Function;
46: ldc #14 // String 99
48: invokeinterface #9, 2 // InterfaceMethod java/util/function/Function.apply:(Ljava/lang/Object;)Ljava/lang/Object;
53: invokevirtual #13 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
56: invokevirtual #15 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
59: areturn
private static java.util.Date lambda$test$2();
Code:
0: new #16 // class java/util/Date
3: dup
4: invokespecial #17 // Method java/util/Date."<init>":()V
7: areturn
private static java.lang.String lambda$test$1(java.lang.String);
Code:
0: aload_0
1: invokevirtual #18 // Method java/lang/String.toUpperCase:()Ljava/lang/String;
4: areturn
private static java.lang.Integer lambda$new$0(java.lang.String);
Code:
0: aload_0
1: invokestatic #19 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
4: invokestatic #20 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
7: areturn
}
Lambdas and invokedynamic
The translation of a lambda expression to be bytecode will be performed in below steps.
- Generate an invokedynamic call site (lambda factory), which when invoked returns an instance of the Functional Interface defined.
- Create a static method which included the lambda expression will be invoked through the invokedynamic instruction.
If you want to know how invokedynamic works, "Using Invoke Dynamic to Teach the JVM a New Language" is good for us.
I had completely gothrow your post and it is full of innovative information.thanks for sharing this.
ReplyDeleteAndroid Training in Chennai
Android Training in Tambaram
JAVA Training in Chennai
Python Training in Chennai
Hadoop Training in Chennai
IOS Training in Chennai
Android Training in Chennai
Android Training in T Nagar
This concept is very superb and your effort is very good. Eagerly waiting for your next post for developing my knowledge...
ReplyDeleteTableau Training in Chennai
Tableau Course in Chennai
Pega Training in Chennai
Spark Training in Chennai
Oracle DBA Training in Chennai
Excel Training in Chennai
Power BI Training in Chennai
Linux Training in Chennai
Oracle Training in Chennai
Unix Training in Chennai
Thanks for sharing such useful information
ReplyDeleteIOT Training in Chennai
PHP Training in Chennai
Wordpress Training in Chennai
A very impressive blog, this blog gives more useful information.
ReplyDeleteAWS Training in Bangalore
AWS Training in Chennai
AWS Training in BTM
AWS Training in Marathahalli
Best AWS Training in Marathahalli
Data Science Courses in Bangalore
DevOps Training in Bangalore
PHP Training in Bangalore
DOT NET Training in Bangalore
Spoken English Classes in Bangalore
Great experience for me by reading this blog. Thank you for the wonderful article.
ReplyDeleteAngularjs course in Chennai
Angularjs Training in Bangalore
angular training in bangalore
Angular Training in hyderabad
angular course in bangalore
angularjs training in marathahalli
Web Designing Course in bangalore
python training in Bangalore
angularjs training institute in bangalore
best angularjs training in bangalore
Every day I always visit sites to obtain the best information for materials research I was doing.......
ReplyDeleteWeb Designing Course Training in Chennai | Web Designing Course Training in annanagar | Web Designing Course Training in omr | Web Designing Course Training in porur | Web Designing Course Training in tambaram | Web Designing Course Training in velachery
valuable blog,Informative content...thanks for sharing, Waiting for the next update…
ReplyDeleteStudy Abroad Consultants in Kerala
study abroad consultants in thrissur
Study Abroad Consultants in Calicut
abroad job consultancy in coimbatore
abroad job consultancy in calicut
abroad job recruitment agencies in calicut
study abroad
study in poland
study in europe
education in germany
GlassWire Elite Activation Key provides you fantastic info and simplifies the system identification that at which the infected document GlassWire Pro Elite Crack
ReplyDeleteReimage License Key 2022 updated lists Reimage PC Repair. Reimage License Key Is LifeTime Activate Daily update Keys. Website
ReplyDeleteAt MyMiniFactory, you'll be able to|you probably can} download over 50,000 free 3D printing fashions. Therefore, in case you are into gaming miniatures, you might wish rabbit supplies to begin right here. Of course, you’ll have to have probably the greatest 3D printers for miniatures to get the best print high quality. Previously, the Zortrax library of free STL files was built-in into their Z-Suite software program and thus only available to Zortrax 3D printer owners. The library has now been separated from the software program and made freely available on-line by the company.
ReplyDelete