Lambda Expression in JAVA--Part 1

Hey, hello folks. How's the thing is going? Hope you guy's are doing well in your life,if not then don't worry all will be good soon. So my name is "Sumit Srivastava" and I am MCA passout of 2019 batch.

 This is my first ever Technical Blog which i am writting to share some concept learnings on JAVA. So today i am taking a very important feature of JAVA 8. Here we go.


Lambda Expression

What is Lambda Expression?

It is one of the most important feature of java which is introduced in Java SE8. It is used to provide a short and clear way to represent one method interface by using expression. Its a block of code similar to function.

Syntax:

parameter -> expression


Java lambda expression syntax consisted of three components:-

  • Argument-list (It can be empty/non-empty)
  • Arrow-token (It is basically act as a bridge between Argument list and expression)
  • Body ( We write expressions, statements, for lambda expression,gives implementation of functional interface)
Lambda Expression can be of no argument, one argument,two argument as well, lets see an image mentioned below:-


                                                                        Lambda Syntax

Where to Use?

It is useful in Collection library and helps to iterate and extract collection data. Compiler treated it as a method,so it does'nt provide any  .class file for this.


It is used to provide implementation of a interface, i.e Functional Interface, so we do not need to write the method again for the implementation purpose,we can use lambda expression and gives implementaion of that method. Therefore it save some line of codes as well. So obviously if it is saving line of code so it is also saving our time also to right that,right??


For providing implementation we use a block of code as an expression in syntax of lambda expression.
So, now the question is,what is functional interface ?


What is Functional Interface?

A interface which is having only one abstract method is called functional interface. Java has given a annotation names @FunctionalInterface. It can be placed to tell compiler that our interface is not a normal interface,it is a functional interface. So just we need to place a annotation,that's all.
Although its not mandatory to use this annotation(it is optional)but its a good programming approach to write a interface to avoid uncessary mess up of adding more methods as part of interface.

As this interface is having only one Single Abstract Method in it so it is also known as SAM interface.

Ex: Runnable interface is an example of Functional interface as it is having only run() declare in it.

Example:-

@FunctionalInterface  // optional                   
interface Runnable{

void run();
}

Lets understand it with a simple diagram:


In the above mentioned image, House is considered as a Functional Interface which is having only one Owner of the house(so it is only one abstract),and tenant(default method) and pay(static) inside it.

With this concept we got to understand the diffrence between Interface and Functional Interface.

We will learn it with example in next blog.
Keep in touch







Comments

Popular posts from this blog

Aurat- Pyar ka Sagar

Multithreading at a Glance - Part 1

Two Best Friends