Lambda Expression:
Lambda expression is an anonymous method or function without declaration. It is useful to write short and less code because of this it avoid unnecessary lengthy code.
Lambda expression is much readable, productive and reliable code.
Lambda Expression make is easier to use functional programming.
It is the first step towards the functional programming.We can pass lambda expression as it is an object and execute when required.
Lambda feature is not new but new in java. It was there in Scala, Haskell..the languages which are
used for cloud.
Advantages of Lambda:
- It enables functional programming:We are used to write object oriented programming in java, but with lambda we can write functional programming.
- Readable and concise code:In some cases while development, lambda will remove the boilerplate code that we use to write in certain scenario.
- Easier to use libraries and APIs.
- Lambda enables support for parallel processing: We can use lambda where code runs over multiprocessor environments.
These are few of the advantages...
Lambda Expression has 3 parts:
i. Parameter List
Lambda Expression can have 0 or 1 or more parameters. Parameters are optional.
ii. Lambda Arrow Operator
Lambda arrow operator is “->” and it detach parameters list and body
iii. Lambda Expression Body
Below highlighted in lambda expression’s body
Eg. java4sks ( x -> x*2 ), it means java4sks receives integer as and then returns the multiplication of that integer.
In java or programming language we deals with data types. Like “java4sks” is of java.lang.String type.In the same way ,
What is the type of below mentioned lambda expression?
() -> System.out.println(“hi lambda….”);
Above statement has no parameters and return some result. So its type would be “java.lang.Runnable” … Functional Interface.
What is Functional Interface ?
Functional Interface is just like SAM interface, which means it has only one abstract method.
(Single Abstract Method). Java8 API has defined many Functional Interfaces.
In the next post, you will see some examples of lambda expression and discuss it in detail...