lambda, functional interface in java8

Lambda expression is new added in java8. It’s actually a functional interface instance when evaluated.

So lambda can be passed in as parameter and returned to simulate function is the first class of java. Lambda expression is defined as parameter list → expression or block body. Block body is surrounded with curly braces and uses return to give the value.

Functional interface is an interface with only one abstract method. Default methods are not counted as abstract. When a Functional interface type is required, a lambda expression can be passed in.

// a no arg functional interface
interface Func0<T> {
    T run(); // the abstract method
	default T apply() {
		return run();
	}
}

void f(Func0<Integer> g) {
	System.out.println(g.run());
}

f(() -> 1); // pass in lambda as lambda will be converted to instance of Func0