写的很全很好的一个博客: http://blog.csdn.net/ioriogami/article/details/12782141
什么是函数接口: http://www.lambdafaq.org/what-is-a-functional-interface/ 肤浅的理解就是:函数注解的接口是这样一个类型,这个类型的作用相当于一个函数;这个接口定义切且只定义了一个抽象方法(实例方法),这个方法不可以和object方法重复。
单看这两个博客理解起来有些吃力,看看java1.8中对FounctionalInterface的解释:
/* * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.lang; import java.lang.annotation.*; /** * An informative annotation type used to indicate that an interface * type declaration is intended to be a <i>functional interface</i> as * defined by the Java Language Specification. * :functionalInterface注解用来指明一个接口类型是functional interface,这句话好像没什么用 * Conceptually, a functional interface has exactly one abstract * method. Since {@linkplain java.lang.reflect.Method#isDefault() * default methods} have an implementation, they are not abstract. If * an interface declares an abstract method overriding one of the * public methods of {@code java.lang.Object}, that also does * <em>not</em> count toward the interface's abstract method count * since any implementation of the interface will have an * implementation from {@code java.lang.Object} or elsewhere. *:一个函数注解借口准确的说,只有一个抽象方法,且这个方法不能是重写的Object的public方法(不计入does not count toward) * <p>Note that instances of functional interfaces can be created with * lambda expressions, method references, or constructor references. *:一个函数注解借口可以使lambda表达式、方法的引用、或者构造函数的引用 * <p>If a type is annotated with this annotation type, compilers are * required to generate an error message unless: *:使用这个注解的时候,为了使编译器不报错,至少要满足: * <ul> * <li> The type is an interface type and not an annotation type, enum, or class. * <li> The annotated type satisfies the requirements of a functional interface. * </ul> *:type是借口而不是注解、枚举、类 * <p>However, the compiler will treat any interface meeting the * definition of a functional interface as a functional interface * regardless of whether or not a {@code FunctionalInterface} * annotation is present on the interface declaration. *:这里描述没用使用functionalInterface注解却被认作是函数注解接口的情况:meeting the definition of a functional interface即满足了函数接口的定义;
* 也就是说,FunctionalInterface注解其实可以省略不写,感觉好像被别人耍了一样,FunctionalInterface就是个概念 * @jls 4.3.2. The Class Object * @jls 9.8 Functional Interfaces * @jls 9.4.3 Interface Method Body * @since 1.8 */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface FunctionalInterface {}
简单的例子:
Runnable接口早就有了,FunctionalInterface是1.8才出的,Runnable接口只有一个抽象函数,满足FounctionalInterface的定义,属于上面提到的特殊情况,在1.8中加上了@FunctionalInterface注解
Thread th=new Thread(()->System.out.println("hello")); th.start();
有了上面的理解后,可以自己写一个Lambda的例子:运行结果是class java.lang.String
package com.lambda; import java.util.concurrent.Callable; public class HelloLambda { public void function(Callable callable){ Object result=null; try {
// 这里使用的callable并没有单独开辟线程进行执行。开辟新线程执行需要使用Thead 的 start方法。 result = callable.call(); System.out.println(result.getClass().toString()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { HelloLambda hello=new HelloLambda(); hello.function(()->new String("hello")); } }