stock market

Functional Programming in Java Using Lambda Expressions

Spread the love

A lambda expression is a short and concise way to represent an anonymous function—a function without a name .

Key Features
  • No method name
  • No return type (inferred by compiler)
  • Used with functional interfaces
  • Reduces boilerplate code
1.Why Lambda expression required

Lambda expression required to enable functional programming ,lambda expression allow you to define small functions without naming them . this is useful when the function is simple and used only once or a few times .

Lambda Syntax :

  • Remove modifier , return type , and method Name .
  • Lambda expression can take any number of parameters , if multiple parameters present . then these parameters should be separated with comma .

2.Lambda Expression Example

Example 1: Addition of Number

Without Lambda Expressions

With Lambda Expressions

Example 2: Find the length of String

without Lambda Expressions

With Lambda Expression

if single statements are there then curly bracket not required , if body contained multiple lines of code then curly bracket required . if lambda expressions return something then we can return keywords .

What is Functional Interface

A Functional Interface that contains exactly one abstract method . and any number of default and static method can contains .

What is the purpose of Functional Interface

The purpose of functional interface in java is to provide a target for lambda expressions ,enabling functional programming within the language . Functional interfaces simplify the development process by allowing you to write more concise and readable code.

Key purpose of Functional Interface

1. Enable Lambda Expressions

Functional interfaces are designed to work with lambda expressions, which provide a clear and concise way to represents instance of single method interfaces . Functional interfaces are the foundation of lambda expression in java .

2. Functional Interface with Interfaces

if an interface extends functional interface and child interface does not contain abstract method then child is always functional interface .

Functional Interface with Inheritance

Case-1 : if an Interface extends functional interface and child interface does not contain any abstract method , the child interface is always a functional interface .

Case-2 : if the child Functional Interface , define exactly same parent interface abstract method then it will work fine .

Case-3: In the child Functional Interface we can not define any new abstract method otherwise we will get compile time error .

Once you compile the above code you will get Invalid @functionalInterface annotation childpayment is not a functional interface .

Case-4: Normal Interface Can extends functional interface

Invoke Lambda Expression by using Functional Interface

Now we will see some example based on the above concepts .

program 1: write a program to find the addition of two number

Program-2: Write a program to find the Square of given Number

Program-3 : write a program to create a Thread Using Lambda Expression

Leave a Reply

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