【238期】Java 8 中 Lambda 实现原理及源码剖析!
点击上方“Java精选”,选择“设为星标”
别问别人为什么,多问自己凭什么!
下方有惊喜,留言必回,有问必答!
每天 08:15 更新文章,每天进步一点点...
为了支持函数式编程,Java 8引入了Lambda表达式,那么在Java 8中到底是如何实现Lambda表达式的呢? Lambda表达式经过编译之后,到底会生成什么东西呢? 在没有深入分析前,让我们先想一想,Java 8中每一个Lambda表达式必须有一个函数式接口与之对应,函数式接口与普通接口的区别,可以参考前面的内容,那么你或许在想Lambda表达式是不是转化成与之对应的函数式接口的一个实现类呢,然后通过多态的方式调用子类的实现呢,如下面代码是一个Lambda表达式的样例:
@FunctionalInterface
interface Print<T> {
public void print(T x);
}
public class Lambda {
public static void PrintString(String s, Print<String> print) {
print.print(s);
}
public static void main(String[] args) {
PrintString("test", (x) -> System.out.println(x));
}
}
@FunctionalInterface
interface Print<T> {
public void print(T x);
}
class Lambda$$0 implements Print<String> {
@Override
public void print(String x) {
System.out.println(x);
}
}
public class Lambda {
public static void PrintString(String s,
Print<String> print) {
print.print(s);
}
public static void main(String[] args) {
PrintString("test", new Lambda$$0());
}
}
@FunctionalInterface
interface Print<T> {
public void print(T x);
}
public class Lambda {
final class Lambda$$0 implements Print<String> {
@Override
public void print(String x) {
System.out.println(x);
}
}
public static void PrintString(String s,
Print<String> print) {
print.print(s);
}
public static void main(String[] args) {
PrintString("test", new Lambda().new Lambda$$0());
}
}
@FunctionalInterface
interface Print<T> {
public void print(T x);
}
public class Lambda {
public static void PrintString(String s,
Print<String> print) {
print.print(s);
}
public static void main(String[] args) {
PrintString("test", new Print<String>() {
@Override
public void print(String x) {
System.out.println(x);
}
});
}
}
Compiled from "Lambda.java"
public class Lambda {
public Lambda();
public static void PrintString(java.lang.String, Print<java.lang.String>);
public static void main(java.lang.String[]);
private static void lambda$0(java.lang.String);
}
@FunctionalInterface
interface Print<T> {
public void print(T x);
}
public class Lambda {
public static void PrintString(String s,
Print<String> print) {
print.print(s);
}
private static void lambda$0(String s) {
}
public static void main(String[] args) {
PrintString("test", (x) -> System.out.println(x));
}
}
Exception in thread "main" java.lang.ClassFormatError: Duplicate method name&signature in class file Lambda
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
Compiled from "Lambda.java"
public class Lambda {
public Lambda();
public static void PrintString(java.lang.String, Print<java.lang.String>);
private static void lambda$0(java.lang.String);
public static void main(java.lang.String[]);
private static void lambda$0(java.lang.String);
}
@FunctionalInterface
interface Print<T> {
public void print(T x);
}
public class Lambda {
public static void PrintString(String s, Print<String> print) {
print.print(s);
}
private static void lambda$0(String x) {
System.out.println(x);
}
public static void main(String[] args) {
PrintString("test", /**lambda expression**/);
}
}
public static CallSite metafactory(MethodHandles.Lookup caller,
String invokedName,
MethodType invokedType,
MethodType samMethodType,
MethodHandle implMethod,
MethodType instantiatedMethodType)
throws LambdaConversionException {
AbstractValidatingLambdaMetafactory mf;
mf = new InnerClassLambdaMetafactory(caller, invokedType,
invokedName, samMethodType,
implMethod, instantiatedMethodType,
false, EMPTY_CLASS_ARRAY, EMPTY_MT_ARRAY);
mf.validateMetafactoryArgs();
return mf.buildCallSite();
}
final class Lambda$$Lambda$1 implements Print {
private Lambda$$Lambda$1();
public void print(java.lang.Object);
}
final class Lambda$$Lambda$1 implements Print {
private Lambda$$Lambda$1();
Code:
0: aload_0
1: invokespecial #10 // Method java/lang/Object."<init>":()V
4: return
public void print(java.lang.Object);
Code:
0: aload_1
1: checkcast #14 // class java/lang/String
4: invokestatic #20 // Method Lambda.lambda$0:(Ljava/lang/String;)V
7: return
}
@FunctionalInterface
interface Print<T> {
public void print(T x);
}
public class Lambda {
public static void PrintString(String s, Print<String> print) {
print.print(s);
}
private static void lambda$0(String x) {
System.out.println(x);
}
final class $Lambda$1 implements Print{
@Override
public void print(Object x) {
lambda$0((String)x);
}
}
public static void main(String[] args) {
PrintString("test", new Lambda().new $Lambda$1());
}
}
作者:让猪再飞会
https://www.cnblogs.com/WJ5888/p/4667086.html
3000+ 道面试题在线刷,最新、最全 Java 面试题!
【230期】Spring Boot 集成 Elasticsearch7.6 实现高亮分词及简单查询
【231期】Elasticsearch 在各大互联网公司大量真实的应用场景案例
【232期】面试官:如何保护 Spring Boot 配置文件敏感信息?
【233期】Java8 stream 处理 List 集合的相同部分(交集)、去重!
【234期】新来的同事问我 where 1=1 是什么意思?
【235期】不同并发场景下 LongAdder 与 AtomicLong 如何选择?
最近有很多人问,有没有读者&异性交流群,你懂的!想知道如何加入。加入方式很简单,有兴趣的同学,只需要点击下方卡片,回复“加群”,即可免费加入交流群!