Java Lambda 表达式源码分析,我就不信你还吃不透Java的泛型

本文深入探讨Java Lambda表达式的实现原理,通过字节码分析,揭示其与匿名内部类的区别。同时,文章介绍了方法引用的概念,并以Integer::sum为例,说明如何使用方法引用。此外,还讨论了函数式接口的特性,指出即使未标注@FunctionalInterface,只要满足条件也可视为函数式接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ToIntBiFunction<Integer, Integer> function = (n1, n2) -> n1 + n2;

System.out.println(function.applyAsInt(1, 2));

ToIntBiFunction<Integer, Integer> function2 = Integer::sum;

System.out.println(function2.applyAsInt(1, 2));

}

大致形式就是 (param1, param2, param3, param4…) -> { doing…… };

函数式接口


首先要从 FunctionalInterface 注解讲起,详情见 Annotation Type FunctionalInterface 。

An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification. Conceptually, a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface’s abstract method count since any implementation of t

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值