Syntax: Argument List Arrow Token Body Note: Functional Interfaces
Syntax: Argument List Arrow Token Body Note: Functional Interfaces
System.out.println(doMathOperation(2, 1, multiplication));
}
Syntax:
with type
(int a, int b) > a + b
declaration
no type
(a, b) > a + b
declaration
with return
(int a, int b) > {return (a + b)}
statement
() > 42 no arguments
{System.out.print(
(String s) > returns nothing
s);}
– A lambda expression can have zero, one or more parameters.
– No need to declare type of a parameter.
– No need to write parenthesis around parameter if there is only one parameter
(required if more).
– No need to use curly braces in expression body if there is only one statement
(required if more).
– No need to use return keyword if there is only one statement. The compiler
returns the value automatically.
>>> More details at: Java 8 – Lambda Expressions
Functional Interfaces
Functional Interfaces is an interface with only one abstract method inside.
http://javasampleapproach.com/java-tutorial/java-8#Lambda_Expressions 2/8
28/05/2018 Java 8 - JavaSampleApproach
@FunctionalInterface annotation is used to annotate for Functional Interface. So
compiler will throw errors when the interface is not a valid Functional Interface.
@FunctionalInterface
public interface MyFuncInterface {
void doWork();
void doAnotherWork();
}
Apply Functional Interfaces:
@FunctionalInterface
public interface MyFuncInterface {
void doWork();
}
public class MainApp {
@Override
public void doWork() {
System.out.println("invoke Function using Anonymous Inner Class");
}
});
For Functional Interface with only one method, we can make code lean and
beautiful with Lambda Expression, it seem like that we pass a method as
argument to a function.
>>> More details at: Java 8 – Functional Interfaces
Method References
http://javasampleapproach.com/java-tutorial/java-8#Lambda_Expressions 3/8