This post will explain one of the most important features in Java 8 Lambda Expressions. This feature is also available in the OCAJP 8 Java certification test. You should be able to understand the concepts and practice answering all questions in the real exam. The OCAJP 8 exam simulator covers more questions using lambda expressions.
What is Lambda Expression?
Lamdba Expressions are the new feature in JDK 8
Java is significantly enhanced by lambda expressions and their related features because of two main reasons.
They first add syntax elements that increase the expressiveness of the language.
Second, new capabilities were added to the API library by the addition of lambda expressions.
Beyond the many benefits that lambda expressions provide to the language, there are also other benefits.
They are an important addition to Java. In recent years, lambda expressions has been a key focus in computer language design. They have been included in languages like C# and C++.
Before you learn about Lamdba Expressions, you need to understand what a functional interface is.
What is Functional Interface?
A functional interface is an interface that contains one abstract method. It can contain any number of static and default methods.
Example:
Interface MyNumber getValue ( );
MyNumber is a functional interface that contains one abstract method.
Let’s take a look at another example
Example:
interface MyValue getValue ( );printValue ( );MyValue does not have a functional interface as it contains two abstract methods.
Example:
interface Sample print( );static void sayHello() System.out.println(“Hello World ” ); Sample is a functional interface because it contains only one abstract method.
Lambda Expressions
A lambda expression can be described as an anonymous (or unnamed) method.
It is used to implement a method that has been defined by a functional interface.
A lambda expression is assigned as a functional interface reference.
Lambda expressions cannot be executed by itself.
Lambda expression is executed when you call functional interface method using interface reference.
A lambda expression is a form anonymous class.
Closures are also a common term for lambda expressions.
The lambda expression introduces new syntax elements and operators to the Java language.
Sometimes, the new operator is called the lambda operator or arrow operator.
It divides a lambda expression into two parts. The left side contains any parameters that are required
by the lambda expression. (If no parameters are required, an empty parameter list will be used. The lambda body is located on the right. It specifies the actions for the lambda expression.
Java has two types of lambda body. The first type is a single expression, while the second type is a block of code.
Let’s write first lambda example.interface MyNumber int getValue();public class SupplyDemo public static void main(String[] args) MyNumber num1 = () -> 9;MyNumber num2 = () -> return 5; ;System.out.println(num1.getValue()); //prints 9System.out.println(num2.getValue()); //prints 5
Look at the functional interface declaration when you write lambda expressions.
GetValue doesn’t accept parameters. Parenthesis are therefore mandatory.
If functional interface method does not take any parameters, parenthesis must be included in the parameter list for lambda expression.
Take a look at the functional interface method return type. It returns int. Also, the lambda expression should return int.
Curly braces are not required if lambda expression only contains one statement. Curly braces are mandatory for lambda expressions that contain more than one statement.
If lambda expression only contains one statement, the lambda expression will return it by default.
Even if there are more than one statement, the lambda expression should end in semicolon
You can use Lambda expression to write multiple implementations of the functional interface method. However, it must be compatible with the method declaration.
Calling num1.getValue ( ) will execute Lambda expression and return value 9.
Calling num2.getValue ( ) will execute Lambda expression and return value 5.
Let’s understand another exampleinterface NumericTest boolean test(int n);class LambdaDemo2 public static void main(String args[])// A lambda expression that tests if a number is even.NumericTest isEven = (n) -> (n % 2)==0;if(isEven.test(10)) System.out.println(“10 is even”);if(!isEven.test(9)) System.out.println(“9 is not even”);// Now use a lambda expression that tests if a number// is non-negative.NumericTest isNonNeg = (n) -> n >= 0;if(isNonNeg.test(1)) System.out.println(“1 is non-negative”);if(!isNonNeg.test(-1)) System.out.println(“-1 is negative”);
This program produces s.
