Lambda Expressions = ‘->’

Java lambda expression is an enhanced version of an anonymous class that does not have a name and does not belong to any class. though The concept of a lambda expression is coming from a language known as Scala. Lambda expression provides the implementation of a functional interface. We can also say that lambda expression is achieved only with a functional interface.

 However, It is a new feature of the Java 8 programming language. Anonymous class, functional interface, and method references are the concepts associated with a lambda expression. In lambda expressions, we use the “->” (Arrow Operator) symbol. 

Syntax of Lambda Expression:

  • (Parameters) -> {expression body};
  • In round parentheses, we can pass arguments zero to multiple depending upon type and sequence set in the method of functional interface.
  • Arrow operator(->): It is introduced in java through The expression that divides it into two parts known as Parameters and body. 
  • In the expression body, we override the method of functional inheritance.

Characteristics of Lambda expression:

  1. Optional Type Declarations: When declaring parameters in The Expression we do not need to declare their types as the compiler can infer them from their values. 
  2. Curly Braces (Optional): If the body contains only one statement there is no need to give curly braces.
  3. Optional Parentheses Around parameters: When only a single parameter is declared, we do not need to place it in parentheses.
  4. Optional return keyword: When the expression returns a value and it is not wrapped inside curly braces, then we do not need a return statement. 

Basis:

Before taking deep dive into the expression, let us overlook some concept on which lambda expression is residing. 

  1. Anonymous class: 
  • Anonymous class in the inner class has no name.
  • It should be declare inside another class.
  • Anonymous classes are define inside another class.
  1. Functional Interface: 
  • The single abstract method in a new version of java is – functional interface.
  • Constructors are not present in interfaces.
  • In functional interfaces, we have only one method and this method is implicitly public and abstract.
  • When we have to use the expressions we use functional interfaces.
  • Sometimes we also used annotation @FunctionalInterface before the interface to indicate that the interface is functional.
  •  It is not compulsory to use functional annotation.

1) Example without the expression:

In the following code, there is a functional interface that consists of only one method and an anonymous class. In an anonymous class, we override the method of the functional interface as it is an abstract method. 

Output:

2) Same Example with a lambda expression:

In the following code we used the expression as mentioned early we need a functional interface to implement lambda expression. Interface Book{ } 

Is a functional interface as it contains only one method which is void select( );.In this expression the round bracket (  ) indicates to select the method and using Arrow operator  -> we override the functional interface method void select( );  

Output:

3) Lambda Expression Use as Argument:

If we have to do addition, subtraction, multiplication, and division without defining methods for different operations then we use a lambda expression. In the following code for addition, subtraction, multiplication, and division we can not define different methods. Instead of this, we declared one method in the functional interface which takes parameters as an integer type. We pass this expressions as arguments to the function. At the same time, we pass the type of operation sign in the lambda expression which we have to do.

Output:

Lambda parameters:

1. Zero parameters: In this type, we can not pass any parameter to lambda expression. If we have to pass any parameter to the lambda expression we have to set the type of parameter.

Example: In the following code we can not pass any argument or parameter.

Output:

2. One parameter: In this type, the lambda expression will take only one parameter as we set it to take only one parameter. The type of parameter which we are going to take is to be set in the method of functional inheritance.

If we take a parameter like an integer then set to int. for text use string, or floating-point number use double.

Example: so, In this code, we set the type of parameter to be taken as an integer by setting the type ‘int’ in the method of functional interface. At the same time, we set a parameter to the lambda expression which is an integer type.

Output:

3. Multiple parameters: Takes more than one parameter. In this type of lambda expression, we can set different types of values for different inputs of parameters.

Example: In the following code we set one parameter to string whereas another parameter to an integer.

Output:

Advantages:

  1. Lambda expression enables functional programming.
  2. We also get some readable and concise code.
  3. Easier-to-use APIs and libraries.
  4. Lambda enables support for parallel processing.

Disadvantages:

  1. Difficult to read and understand.
  2. Sometimes lambda expressions are unfamiliar to many java programmers.

Conclusion:

If we have to work with functional interface then Lambda expression Is very important. As it enables functional programming and parallel processing. Lambda expressions convert long code into a short format. But at the same time, it may be difficult to understand if someone is not familiar with this expressions.

Written By: Yash Zadap

Reviewed By: Soutik Maity

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

Your email address will not be published. Required fields are marked *