Lambdas are purely a call-site construct: the recipient of the lambda does not need to know that a Lambda is involved, instead it accepts an Interface with the appropriate method.

In other words, you define or use a functional interface (i.e. an interface with a single method) that accepts and returns exactly what you want.

Since Java 8 there is a set of commonly-used interface types in java.util.function.

For this specific use case there's java.util.function.IntBinaryOperator with a single int applyAsInt(int left, int right) method, so you could write your method like this:

static int method(IntBinaryOperator op){
    return op.applyAsInt(5, 10);
}

But you can just as well define your own interface and use it like this:

public interface TwoArgIntOperator {
    public int op(int a, int b);
}

//elsewhere:
static int method(TwoArgIntOperator operator) {
    return operator.op(5, 10);
}

Then call the method with a lambda as parameter:

public static void main(String[] args) {
    TwoArgIntOperator addTwoInts = (a, b) -> a + b;
    int result = method(addTwoInts);
    System.out.println("Result: " + result);
}

Using your own interface has the advantage that you can have names that more clearly indicate the intent.

Answer from Joachim Sauer on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_lambda.asp
Java Lambda Expressions
The variable's type must be an interface with exactly one method (a functional interface). The lambda must match that method's parameters and return type. Java includes many built-in functional interfaces, such as Consumer (from the java.util ...
🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › lambdaexpressions.html
Lambda Expressions (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
For example, the lambda expression directly accesses the parameter x of the method methodInFirstLevel. To access variables in the enclosing class, use the keyword this.
Top answer
1 of 16
337

Lambdas are purely a call-site construct: the recipient of the lambda does not need to know that a Lambda is involved, instead it accepts an Interface with the appropriate method.

In other words, you define or use a functional interface (i.e. an interface with a single method) that accepts and returns exactly what you want.

Since Java 8 there is a set of commonly-used interface types in java.util.function.

For this specific use case there's java.util.function.IntBinaryOperator with a single int applyAsInt(int left, int right) method, so you could write your method like this:

static int method(IntBinaryOperator op){
    return op.applyAsInt(5, 10);
}

But you can just as well define your own interface and use it like this:

public interface TwoArgIntOperator {
    public int op(int a, int b);
}

//elsewhere:
static int method(TwoArgIntOperator operator) {
    return operator.op(5, 10);
}

Then call the method with a lambda as parameter:

public static void main(String[] args) {
    TwoArgIntOperator addTwoInts = (a, b) -> a + b;
    int result = method(addTwoInts);
    System.out.println("Result: " + result);
}

Using your own interface has the advantage that you can have names that more clearly indicate the intent.

2 of 16
78

To use Lambda expression you need to either create your own functional interface or use Java functional interface for operation that require two integer and return as value. IntBinaryOperator

Using user defined functional interface

interface TwoArgInterface {

    public int operation(int a, int b);
}

public class MyClass {

    public static void main(String javalatte[]) {
        // this is lambda expression
        TwoArgInterface plusOperation = (a, b) -> a + b;
        System.out.println("Sum of 10,34 : " + plusOperation.operation(10, 34));

    }
}

Using Java functional interface

import java.util.function.IntBinaryOperator;

public class MyClass1 {

    static void main(String javalatte[]) {
        // this is lambda expression
        IntBinaryOperator plusOperation = (a, b) -> a + b;
        System.out.println("Sum of 10,34 : " + plusOperation.applyAsInt(10, 34));

    }
}
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lambda-expressions-parameters
Java - Lambda Expressions Parameters - GeeksforGeeks
July 23, 2025 - One example is java.lang.Runnable. Lambda expressions implement only one abstract function and therefore implement functional interfaces. Predicate interface is an example of a functional interface that has only one abstract method called test(). ... The above is a functional interface that has one abstract method test receiving only one parameter of type T and returns a Boolean value.
🌐
Baeldung
baeldung.com › home › java › lambda expressions and functional interfaces: tips and best practices
Lambda Expressions and Functional Interfaces: Tips and Best Practices | Baeldung
December 16, 2023 - Functional interfaces, which are gathered in the java.util.function package, satisfy most developers’ needs in providing target types for lambda expressions and method references. Each of these interfaces is general and abstract, making them easy to adapt to almost any lambda expression. Developers should explore this package before creating new functional interfaces. ... In addition, we have a method add() in some class UseFoo, which takes this interface as a parameter:
🌐
GeeksforGeeks
geeksforgeeks.org › java › lambda-expressions-java-8
Java Lambda Expressions - GeeksforGeeks
Java lambda expressions, introduced in Java 8, allow developers to write concise, functional-style code by representing anonymous functions. They enable passing code as parameters or assigning it to variables, resulting in cleaner and more readable ...
Published   1 week ago
🌐
Programiz
programiz.com › java-programming › examples › lambda-expression-as-method-parameter
Java Program to pass lambda expression as a method argument
Here, e -> e.toUpperCase() is a lambda expression. It takes all elements of the arraylist and converts them into uppercase. import java.util.ArrayList; import java.util.Arrays; class Main { public static void main(String[] args) { // create an ArrayList ArrayList<String> languages = new ArrayList<>(Arrays.asList("java", "python")); System.out.println("ArrayList: " + languages); // call the foEach() method // pass lambda as argument fo forEach() // reverse each element of ArrayList System.out.print("Reversed ArrayList: "); languages.forEach((e) -> { // body of lambda expression String result = ""; for (int i = e.length()-1; i >= 0 ; i--) result += e.charAt(i); System.out.print(result + ", "); }); } }
🌐
Jenkov
jenkov.com › tutorials › java › lambda-expressions.html
Java Lambda Expressions
In the example above, a lambda expression is used as parameter where the parameter type was the StateChangeListener interface. This interface only has a single method. Thus, the lambda expression is matched successfully against that interface. A single method interface is also sometimes referred to as a functional interface. Matching a Java lambda expression against a functional interface is divided into these steps:
Find elsewhere
🌐
Medium
devcookies.medium.com › a-complete-guide-to-lambda-expressions-in-java-0aea2e1cea42
A Complete Guide to Lambda Expressions in Java
December 3, 2024 - Parameters: Input parameters for the lambda function. Arrow Token (->): Separates the parameter list and the body. Body: The code to be executed. Conciseness: Reduce boilerplate code for anonymous classes.
🌐
CodingNomads
codingnomads.com › java-lambda-expressions
Java Lambda Expressions
Here's the syntax for creating a lambda expression in Java, where you can substitute the variables starting with your_ with your values and add as many parameters as you'd like.
🌐
Medium
medium.com › @marcelogdomingues › java-lambda-expressions-techniques-for-advanced-developersava-lambda-expressions-techniques-for-c1d71c30bb1f
Java Lambda Expressions: Techniques for Advanced Developersava Lambda Expressions: Techniques for…
June 21, 2024 - Arrow Token: The arrow token (->) separates the parameters from the body of the lambda expression. Body: The body can be a single expression or a block of statements. If the body is a single expression, the return keyword and curly braces can be omitted. ... Before lambda expressions, implementing functional interfaces required creating anonymous inner classes. This approach often led to verbose and less readable code. Example: Sorting a List Using an Anonymous Inner Class · import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class
🌐
Javatpoint
javatpoint.com › java-lambda-expressions
Java Lambda Expressions
October 16, 2016 - Java Java provides a new feature called method reference in Java 8. Method reference is used to refer method of functional interface. It is compact and easy form of lambda expression.
🌐
Oracle
oracle.com › java › technical details
Java 8: Lambdas, Part 1
Syntax. A lambda in Java essentially consists of three parts: a parenthesized set of parameters, an arrow, and then a body, which can either be a single expression or a block of Java code. In the case of the example shown in Listing 2, run takes no parameters and returns void, so there are ...
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-lambda-expressions-tutorial-with-examples
Java Lambda Expressions Tutorial with examples
September 11, 2022 - A lambda expression in Java has these main parts: Lambda expression only has body and parameter list. 1. No name – function is anonymous so we don’t care about the name 2. Parameter list 3. Body – This is the main part of the function. 4. No return type – The java 8 compiler is able to infer the return type by checking the code.
🌐
Scaler
scaler.com › home › topics › java › lambda expression in java
Lambda Expression in Java | Scaler Topics
May 4, 2023 - Lambda expression in java is an anonymous (no name) function that does not need to define the data type of input parameters and does not need to have a return type. Lambda expression in java implements the functional interface and it can be ...
🌐
Programiz
programiz.com › java-programming › lambda-expression
Java Lambda Expressions (With Examples)
Hence, the left side of the operator includes an empty parameter. The right side is the lambda body that specifies the action of the lambda expression. In this case, it returns the value 3.1415. In Java, the lambda body is of two types.
🌐
Medium
medium.com › @bubu.tripathy › effective-lambda-expressions-in-java-2d4061dde77a
Effective Lambda Expressions in Java | by Bubu Tripathy | Medium
March 11, 2023 - When using Lambda expressions to ... of the abstract method. The Lambda expression takes the same parameters as the abstract method, and returns a value that represents the result of the method....
🌐
Coding Shuttle
codingshuttle.com › java-programming-handbook › parameters-in-lambda-expression
Parameters in Lambda Expression | Coding Shuttle
April 9, 2025 - These parameters define the data that the lambda expression will operate on. The number and type of parameters in a lambda expression must match the abstract method of the functional interface it implements.
🌐
ScholarHat
scholarhat.com › home
Lambda Expressions in Java: Explained in Easy Steps
September 3, 2025 - Explanation: The correct syntax for a Lambda expression in Java includes the parameter list in parentheses, followed by the arrow (->) and the method implementation in curly braces.
🌐
InfoWorld
infoworld.com › home › blogs › java 101: learn java
Get started with lambda expressions in Java | InfoWorld
November 7, 2019 - Consider lambda expression account -> true. The compiler verifies that the lambda matches Predicate<T>‘s boolean test(T) method, which it does–the lambda presents a single parameter (account) and its body always returns a Boolean value (true).