Java 8在语法上的主要改进就是新增了Lambda Expression以及Method Reference。由于官方网站的介绍稍显罗嗦,而且例子也有些复杂。我这里将提供一些更为浅显、直观的例子来帮助大家理解Java 8新引入的语法特性。
Java 8中的Lambda Expression与C、C++都不太一样。Apple为LLVM Clang新开发了Blocks语法特性,使得GNU99标准C编译器在Clang编译器下就能使用Lambda Expression。而C++则在C++11标准中就引入了Lambda表达式。Clang为Lambda表达式定义了一种新类型——
而C++11则使用[
而Java 8却使用了一种与众不同的方式。你可以自己定义一个接口,然后将该接口引用指向一个Lambda表达式。此接口当然也有限制,即必须是函数接口!什么是函数接口?即一个interface仅有一个抽象方法的接口称为是函数接口(functional interface)。然后,lambda表达式的实体定义为:(<参数列表>) -> {
虽然这种形式有点奇葩,但还好,不算太过麻烦。由于Java比C++或Objective-C来真心啰嗦很多。这次有了Method Reference之后,可以简化很多设计。下面例子也会呈现这点:
package src;
import java.util.ArrayList;
interface MyLambdaFunc {
? ? public void call(int p);
}
class MyClass {
? ?
? ? public static void helloStaticMehtod(int a) {
? ? ? ? System.out.println("Static method value is: " + a);
? ? }
? ?
? ? public void memberMethod(int a) {
? ? ? ? System.out.println("Member method is: " + a);
? ? }
? ?
? ? public void method1(int a) {
? ? ? ? System.out.println("Method 1:" + (a + 1));
? ? }
? ?
? ? public void method2(int a) {
? ? ? ? System.out.println("Method 2: " + (a + 2));
? ? }
? ?
? ? public void method3(int a) {
? ? ? ? System.out.println("Method 3: " + (a + 3));
? ? }
}
public class Main {
? ? public static void main(String[] args) {
? ? ? ? // TODO Auto-generated method stub
? ? ? ? System.out.println("Hello, world");
? ? ? ?
? ? ? ? // Type inference
? ? ? ? ArrayList
? ? ? ? arr.add("hello");
? ? ? ?
? ? ? ? // Lambda Expression test
? ? ? ? final int a = 10;
? ? ? ?
? ? ? ? MyLambdaFunc lambda = (p) -> {
? ? ? ? ? ? System.out.println("The value is: " + (a + p));
? ? ? ? ? ? };
? ? ? ? lambda.call(100);
// Method Reference test
? ? ? ? MyLambdaFunc methodRef = MyClass::helloStaticMehtod;
? ? ? ? methodRef.call(-100);
? ? ? ?
? ? ? ? MyClass mlc = new MyClass();
? ? ? ? methodRef = mlc::memberMethod;
? ? ? ? methodRef.call(-200);
? ? ? ?
? ? ? ? MyLambdaFunc methodList[] = { mlc::method1, mlc::method2, mlc::method3 };
? ? ? ? methodList[0].call(100);
? ? ? ? methodList[1].call(100);
? ? ? ? methodList[2].call(100);
? ? }
}