Java lambda expressions example introduces the way we do functional programming in Java. Java lambda expressions are Java’s first step into functional programming. Thus, a Java lambda expression is a function which can be created without belonging to any class. Let’ understand what lambda expression is and how it works.
Java Lambda Expression Syntax
A lambda expression is characterized by the following syntax.
1 |
parameter -> expression body |
Lambda expression is with the important characteristics like below.
1. Optional type declaration: No need to declare the type of a parameter.
1 2 3 4 5 |
// with type declaration MathOperation addOperation = (int x, int y) -> x + y; // without type declaration MathOperation subOperation = (x, y) -> x - y; |
2. Optional parenthesis around parameter: Single parameter is not required in parenthesis. For multiple parameters, parentheses are required.
3. Optional curly braces: Curly braces are not required in expression body if the body contains a single statement.
1 2 3 4 5 |
// with return statement along with curly braces MathOperation multiOperation = (int x, int y) -> x * y; // without return statement and without curly braces MathOperation divOperation = (int x, int y) -> {return x / y;}; |
4. Optional return keyword: The Java compiler automatically returns the value if the body has a single expression. Curly braces are required to indicate that expression returns a value.
Java Lambda Expressions Example
Create the following main class using Eclipse or any editor if you want, like below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package com.javabycode.demo; public class LambdaExpDemo { public static void main(String args[]) { LambdaExpDemo tester = new LambdaExpDemo(); // with type declaration MathOperation addOperation = (int x, int y) -> x + y; // without type declaration MathOperation subOperation = (x, y) -> x - y; // with return statement along with curly braces MathOperation multiOperation = (int x, int y) -> x * y; // without return statement and without curly braces MathOperation divOperation = (int x, int y) -> {return x / y;}; // without return value and print the output PrintMathOperation printOperation = (int x, int y) -> System.out.println("Print Operation " + (x + y)); System.out.println("15 + 8 = " + tester.operate(15, 8, addOperation)); System.out.println("15 - 8 = " + tester.operate(15, 8, subOperation)); System.out.println("15 x 8 = " + tester.operate(15, 8, multiOperation)); System.out.println("15 / 8 = " + tester.operate(15, 8, divOperation)); printOperation.printOperation(150, 23); } interface MathOperation { int operation(int x, int y); } interface PrintMathOperation { void printOperation(int x, int y); } private int operate(int x, int y, MathOperation mathOperation) { return mathOperation.operation(x, y); } } |
Run main class above in Eclipse
or
Run the command javac LambdaExpDemo.java and the java LambdaExpDemo
It should produce the following output.
That’s it.
Reference
Lambda Expressions
Download source code, click link below
Java-lambda-expressions-example.zip (259 downloads)