Closures in Java with Examples
Last Updated :
05 Jun, 2023
A method is a collection of statements that perform some specific task and return the result to the caller. A method can perform some specific task without returning anything. Methods allow us to reuse the code without retyping the code. In Java, every method must be part of some class that is different from languages like C, C++, and Python. In this article, we will understand one type of function named closures.
Lambda Expression in Java
Before getting into the closures, let’s first understand what a lambda expression is. A lambda expression basically expresses instances of the functional interfaces(An interface with a single abstract method is called a functional interface). An example is java.lang.Runnable.
Functionalities of Lambda Expressions
Lambda expressions implement only abstract functions and therefore implement functional interfaces. Lambda expressions are added in Java 8 and provide the below functionalities:
- Enable to treat functionality as a method argument, or code as data.
- A function that can be created without belonging to any class.
- A lambda expression can be passed around as if it was an object and executed on demand.
The main limitation of the lambda expression is that the scope for the lambda expressions will only be final. That is, we can’t change the value of the variables in the lambda expressions. Suppose we define a lambda expression in which we increment the value of a variable, it simply throws an error. In order to solve this error, the closures have been defined.
Examples of Lambda Expression
Example 1: In this example, we will first implement a lambda expression without a parameter or an argument.
Java
import java.io.*;
interface SalutationInterface {
public String salHello();
}
class GFG {
public static void main(String[] args)
{
SalutationInterface obj = () ->
{
return "Hello, GFGians!" ;
};
System.out.println(obj.salHello());
}
}
|
Example 2: In this example, we will understand how to implement the lambda expression which takes multiple parameters.
Java
import java.io.*;
interface concatStrings {
public String concat(String a, String b);
}
class GFG {
public static void main(String[] args)
{
concatStrings s = (s1, s2) -> s1 + s2;
System.out.println(s.concat( "Hello, " , "GFGians!" ));
}
}
|
Closures in Java
Closures are the inline-function valued expressions which means that they are the class functions with bounded variables. Closures can be passed to another function as a parameter. A closure gives us access to the outer function from an inner function. But as of Java 1.6, Java does not rely on closures or Java does not have closures. Also, anonymous inner classes are not closures in Java. Using closures helps in data privacy and currying (currying means breaking a function with many parameters as a number of functions in a single argument). Though the concept of closure is much more well-defined in Javascript, closures can still be implemented using lambda expressions.
Java Closures Syntax
The syntax to implement a closure is:
(argument_list) -> {func_body}
Examples of the Java Closures
Now, let’s understand how to implement the closures with examples:
Example 1: In this example, we are going to use a custom functional interface to display the month from the number provided.
Java
import java.io.*;
interface NumToMonth {
public String convertToMonth( int x);
}
class GFG {
public static void main(String[] args)
{
NumToMonth obj = new NumToMonth() {
String[] months
= { "Jan" , "Feb" , "Mar" , "Apr" ,
"May" , "Jun" , "Jul" , "Aug" ,
"Sep" , "Oct" , "Nov" , "Dec" };
public String convertToMonth( int n)
{
return (n > 0 && n <= months.length)
? months[n - 1 ]
: null ;
};
};
System.out.println(obj.convertToMonth( 8 ));
}
}
|
Example 2:
Java
@FunctionalInterface
public interface Operation {
void operate( int n);
}
ClosureExample.java package com.javatpoint;
public class GFG {
public static void main(String args[])
{
int a = 12 ;
int b = 88 ;
temp(a, new Operation() {
@Override public void operate( int n)
{
System.out.println( "Result is: " + (n + b));
}
});
}
private static void temp( int i, Operation op)
{
op.operate(i);
}
}
|
Output:
Result is: 100
Closure vs Lambda
Both lambda and closure are quite similar to each other the difference can be understood using the difference between a class and an instance of the class. A Class exists only in the source code, and it doesn’t exist during the runtime. There are only objects of the class type during the runtime. Similarly, Closures are to lambdas as objects are to classes.
Share your thoughts in the comments
Please Login to comment...