Lambda expressions are anonymous functions—methods that don’t have declaration; in other words, access modifier, return type, and so forth. You can use lambda expressions to write a method without having to declare it prior to its usage point. In essence, such expressions enable you to directly call a method; this is helpful in situations where you would need to call your method just once. This article presents a discussion on lambda expressions with code snippets wherever applicable.
What Are Lambda Expressions? Why Should I Use Them?
A lambda expression is a block of code that can have parameters and can be executed later, either once or multiple times. One common use case for lambda expressions is in mathematical computations, when you would need to pass a function as an argument to another. You can learn more on this here: http://www.javaworld.com/article/2092260/java-se/java-programming-with-lambda-expressions.html.
Note that functions are the first class citizens in any functional programming language. You can assign a variable to a function or pass a function as a parameter to another function. JavaScript is a functional programming language that provides these features. Functional programming languages provide support for closure, an extension of the concept of scope. A closure is a function or a reference to a function that can have access to variables within the scope where the function was defined. The Java Programming language provides a good alternative to closure using lambda expression, even though there are distinct differences between the two. In essence, lambda expressions enable us to use functions as first class citizens, much like typical functional programming languages. Although it is not encouraged, you can serialize a lambda expression provided that the target type and the arguments of the lambda expression are serializable.
Here is the syntax for using lambda expressions in Java:
(argument) -> (body)
Hence, the following are some valid lambda expression statements:-
(String str) -> { System.out.println(str); } (int x, int y) -> { return x + y; }
Refer to the following code snippet.
new Thread(new Runnable() { public void run() { System.out.println("Run method called..."); } }).start();
In the preceding code snippet, a new thread instance is created and the start method on the thread instance is called. Note that the run method on the thread instance will be called only when the thread is scheduled. This example doesn’t make use of lambda expressions. Refer to the next code snippet that uses lambda expressions to start a thread instance; the code is much simpler this time.
new Thread( () -> System.out.println("Run method called...") ).start();
Structure of Lambda Expressions
Now that we have some knowledge on what lambda expressions are, why they are used, and the syntax, let’s take a quick tour of what the structure of lambda expressions look like; in other words, how they are organized.
The structure of a lambda expression is comprised of the following:
- An arrow ( -> )
- Argument list
- Body of the lambda expression
Here’s the list of some points you should be aware of when working with lambda expressions.
- A lambda expression is a block of code that can accept zero or multiple parameters. The types of these parameters can be declared explicitly.
- The parameters to a lambda expression are enclosed in parentheses and are separated by using commas.
- The body of a lambda expression can be empty, or it can contain one or more statements. Note that curly braces are not needed if the lambda expression contains a single statement. If the body of a lambda expression is comprised of a single expression, this expression would be evaluated and returned. Here is an example:
() -> System.out.println("Hello World!");
References
Here’s a list of some useful links for further study on this topic:
- http://www.drdobbs.com/jvm/lambda-expressions-in-java-8/240166764
- http://java8.in/java-8-lambda-expression/
- http://www.javaworld.com/article/2092260/java-se/java-programming-with-lambda-expressions.html
Summary
Lambda expressions have been introduced in Java SE 8 and enable you to represent a method interface using an expression. In this article, we discussed lambda expressions—what are they, why they are used, and how lambda expressions can provide functional programming features in the Java Programming Language. Happy reading!