So far, we have focused on objects in Java. Since Java 8, more importance is given to the functional aspects of programming. JavaSoft people realized that doing everything using objects is becoming cumbersome and using functions can be more efficient in certain cases. Lambda expression is the most important feature that brings revolutionary change to the way programming is done in Java. In this section, we will discuss closure in a lambda expression, its declaration, implementation, and example. Before moving to the Java closure let's understand lambda expression in Java.
A lambda expression is a method without a name, access specifier, and return value declaration. Such methods are known as the anonymous method or closure. Basically, it represents the instance of the functional interface. Remember that an anonymous class is a class that does not have a name but for which an object can be created. Similarly, a lambda expression is a method without a name but can be used to perform a task. We can also define it as a parameter.
To understand the concept of the lambda expression, consider the following method.
To convert the above method into a lambda expression, we will remove access specifier public, return type void, the name of the method show. After that write the method as it is.
Observe the empty parentheses. They represent that the method does not have any parameters. After that, we have used an arrow symbol (->) that separates the method header from the method body. We can also write the above lambda expression in a single line:
Suppose, we have to perform the sum of two numbers by using the lambda expression. The lambda expression for the operation will be:
Lambda expressions are divided into two categories.
A closure is a function (or method) that refers to free variables in their lexical context. The function is a block of code with parameters. It may produce a result value (return type). A free variable is an identifier used but not defined by the closure. It can be used without being a method or a class. It means that closure can access variables that are not defined in the parameter list and also assigned to a variable. We can also parse the closure as a parameter.
In other words, a lambda expression becomes a closure when it is able to access the variables that are outside of this scope. It means a lambda can access its outer scope.
Note: Java does not support closures.
The primary goal of closure is it concise the function literals without the pain of anonymous instance and interoperate with existing APIs. We can also perform functional and aggregate operations using Java closure. It helps to protect data and provides currying.
The declaration of Java closure is the same as lambada expression. If we want to declare closure in Java, we should use the following syntax.
Java Closure Example
Let's understand the closure in lambda expression through a Java program. First, we will create an interface with the name Operation.
Let's run the above Java program.
Sum is: 110
Now, we will try to change the value of y inside the operate() method. Let's see what happens.
After making the above changes in the program, again compile and run. It gives a compilation error.
Note that we have not declared the variable y as final but still, the compiler assuming it as a final variable (final int y=90;). So, this concept has been changed in JDK 1.8.
Let's run the above program with JDK 1.7. For changing the compiler in Eclipse IDE, follow the steps given below.
Again, run the above program. It shows the following compilation error.
In order to run the program, make the variable y as final.
After that, run the program again.
Sum is: 110
Therefore, in Java 8 we need not to make the variable final. Java compiler automatically assumes it as a final variable. This concept is called closure in the lambda expression.
Let's write the above program using the lambda expression.
Closure Vs. Lambda
Java lambda expression is a way to define a function inline rather than the standard method of declaration. It provides a concise way to represent a function. While closure is a function that encompasses its surrounding state by referencing fields that are external to its body. Note that both are functions but not all closures are lambdas and not all lambdas are closures.
The difference between closure and lambda is quite equivalent to the distinction between a class and an instance of the class. A class exists only in source code. it doesn't exist at runtime. What exists at runtime are objects of the class type. Closures are to lambdas as objects are to classes. The following table describes the key differences between closure and lambda.
Next TopicJava String Encoding