Java8 - Lamda expressions
What are Lamda expressions?
Lamda expressions is Java's introduction to functional programming. Lamda expressions can be created and passed around like regular objects and executed when needed.
Why do we have Lamda expressions?
Java is often criticised as verbose, with Lamda expressions there is an attempt to decrease this verbosity. They address the bulkiness of the anonymous inner classes.
With lamda expression you can now represent single method interfaces in a simple and concise way. These single method interfaces are often termed as functional interfaces as in Java8. If you do not know about functional interfaces check my other post on the same.
So how do we create these Lamda expression?
Lamda expressions are composed of 3 parts:
1. Argument list
If the body consists of a single line then curlies are not required.
What are Lamda expressions?
Lamda expressions is Java's introduction to functional programming. Lamda expressions can be created and passed around like regular objects and executed when needed.
Why do we have Lamda expressions?
Java is often criticised as verbose, with Lamda expressions there is an attempt to decrease this verbosity. They address the bulkiness of the anonymous inner classes.
With lamda expression you can now represent single method interfaces in a simple and concise way. These single method interfaces are often termed as functional interfaces as in Java8. If you do not know about functional interfaces check my other post on the same.
So how do we create these Lamda expression?
Lamda expressions are composed of 3 parts:
1. Argument list
It is represented like any other method definition like for example () or (int x, int y) etc
2. Arrow token
Represented by the -> symbol
3. BodyIf the body consists of a single line then curlies are not required.
Example of such an expression is
Runnable r = () -> System.out.println("I am running");
It is worth noting that the expressions find their usage in places where there are interfaces with single abstract method. This way, the compiler knows how to exactly match the argument list and the method to the definition in the interface.
If you understand this article it will form the basis for the next article where we will dive into the intricacies of lamda expression
No comments:
Post a Comment
Please use proper blogging etiquette while posting comments.
Note: only a member of this blog may post a comment.