1. The first expression will be used where you get 2 parameters for a method and return a value.

  2. The second expression will be used x -> x * x where you get 1 parameters for a method and return a value.

  3. The third expression ( ) -> x will be used ( ) -> x where you get 0 parameters for a method and return a value.

Let's take the third one. Suppose you have an interface which takes no parameters and returns a value.

static interface RandomMath {
    public int random();
}

Now You want to instantiate this interface along with its implementation. Without using lambda it will be done as below:-

Random random = new Random();
RandomMath randomMath = new RandomMath() {
    @Override
    public int random() {
        return random.nextInt();
    }
};

Using lambda it will be like:-

Random random = new Random();
RandomMath randomMath = () -> random.nextInt(); //the third type.

Similarly, for first two it can be used for methods which take two and one parameters and return a value.

static interface PlusMath {
    public int plus(int a, int b);
}

PlusMath plusMath = (a, b) -> a + b;

static interface SquareMath {
    public int square(int a);
}

SquareMath squareMath = a -> a * a;
Answer from Mritunjay on Stack Overflow
Top answer
1 of 3
9
  1. The first expression will be used where you get 2 parameters for a method and return a value.

  2. The second expression will be used x -> x * x where you get 1 parameters for a method and return a value.

  3. The third expression ( ) -> x will be used ( ) -> x where you get 0 parameters for a method and return a value.

Let's take the third one. Suppose you have an interface which takes no parameters and returns a value.

static interface RandomMath {
    public int random();
}

Now You want to instantiate this interface along with its implementation. Without using lambda it will be done as below:-

Random random = new Random();
RandomMath randomMath = new RandomMath() {
    @Override
    public int random() {
        return random.nextInt();
    }
};

Using lambda it will be like:-

Random random = new Random();
RandomMath randomMath = () -> random.nextInt(); //the third type.

Similarly, for first two it can be used for methods which take two and one parameters and return a value.

static interface PlusMath {
    public int plus(int a, int b);
}

PlusMath plusMath = (a, b) -> a + b;

static interface SquareMath {
    public int square(int a);
}

SquareMath squareMath = a -> a * a;
2 of 3
5

The first two examples are different from the last one. The variables in the function (lambda expression) refer to its parameters.

While in the third example the x refers to variable outside of the lambda expression but within the lexical scope (can be either local variable from the method or instance variable).

Example 1 (typically stream reduce), computes the sum by passing the so far computed sum and next item from the list to lambda function:

 int sum = list.stream().reduce((int x, int y) -> x+y);

Example 2, computes the squares from the elements:

 squares = list.stream().map((int x) -> x*x).collect(Collectors.toList());

Example 3, sets the element to default value if it's null in the list:

 final int x = MY_DEFAULT_VALUE;
 // lambda refers the the variable above to get the default
 defaults = list.stream().map((Integer v) -> v != null ? v : x);

Or better for example 3 is the map atomic methods:

 int x = MY_DEFAULT_VALUE;
 // lambda refers the the variable above to get the default
 map.computeIfAbsent(1, (Integer key) -> x);
 // the same could be achieved by putIfAbsent() of course
 // but typically you would use (Integer key) -> expensiveComputeOfValue(x)
 // ...
 // or quite common example with executor
 public Future<Integer> computeAsync(final int value) {
     // pass the callback which computes the result synchronously, to Executor.submit()
     // the callback refers to parameter "value"
     return executor.submit(() -> computeSync(value));
 }
Top answer
1 of 3
14

Well, the other answers cover what \() -> "something" means in Haskell: an unary function that takes () as argument.

  • What is a function without arguments? – A value. Actually, it can occasionally be useful to think of variables as nullary functions that evaluate to their value. The let-syntax for a function without arguments (which doesn't actually exist) ends up giving you a variable binding: let x = 42 in ...

  • Does lambda calculus have nullary functions? – No. Every function takes exactly one argument. However, this argument may be a list, or the function may return another function that takes the next argument. Haskell prefers the latter solution, so that a b c is actually two function calls ((a b) c). To simulate nullary functions, you have to pass some unused placeholder value.

2 of 3
9

You're misinterpreting what () means in Haskell. It isn't the lack of a value, it is rather the only value of the Unit type (the type itself being referred to by an empty set of parentheses ()).

Since lambdas can be constructed to use pattern matching, the lambda expression \() -> "s" is explicitly saying "create an anonymous function, expecting an input that matches the () pattern". There isn't much point to doing it, but it's certainly allowed.

You can use pattern matching with lambdas in other ways as well, for example:

map (\(a, b) -> a + b) [(1,2), (3,4), (5,6)] -- uses pattern matching to destructured tuples

map (\(Name first _) -> first) [Name "John" "Smith", Name "Jane" "Doe"] -- matches a "Name" data type and its first field

map (\(x:_) -> x) [[1,2,3], [4,5,6]] -- matches the head of a list
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › csharp › language-reference › operators › lambda-expressions
Lambda expressions - Lambda expressions and anonymous functions - C# reference | Microsoft Learn
January 24, 2026 - The return value of the lambda (if any) must be implicitly convertible to the delegate's return type. A lambda expression doesn't have a type because the common type system has no intrinsic concept of "lambda expression."
🌐
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 - Lambda syntax only requires parentheses ... there is no parameter at all. That’s why it’s safe to make our code a little bit shorter, and to exclude parentheses when there is only one parameter.
🌐
Python Examples
pythonexamples.org › python-lambda-function-without-arguments
Python Lambda Function without Arguments
In the following program, we define ... to a variable named greeting. Call the lambda function with greeting() and print the returned value. greeting = lambda: "Hello World" # Call the lambda function result = greeting() print(result)...
🌐
Jenkov
jenkov.com › tutorials › java › lambda-expressions.html
Java Lambda Expressions
Notice how the parentheses have no content in between. That is to signal that the lambda takes no parameters. If the method you are matching your Java lambda expression against takes one parameter, you can write the lambda expression like this:
🌐
CodingTechRoom
codingtechroom.com › question › write-lambda-expression-ignored-parameters-java
How to Write a Lambda Expression in Java When Parameters Are Ignored? - CodingTechRoom
Java lambda expressions by design ... a workaround, but can burden readability and understanding. Use a lambda expression without parameters by using empty parentheses: `setRowFactory(() -> new TableRowCustom());`...
Find elsewhere
🌐
Cppreference
en.cppreference.com › w › cpp › language › lambda.html
Lambda expressions (since C++11) - cppreference.com
This user-defined conversion function is only defined if the lambda expression has no captures  and has no explicit object parameter(since C++23). It is a public, constexpr,(since C++17) non-virtual, non-explicit, const noexcept member function of the closure object.
🌐
GeeksforGeeks
geeksforgeeks.org › problems › no-parameter-lambda-expression › 1
No Parameter Lambda Expression | Practice | GeeksforGeeks
You need to use lambda expression to print "Hello".You need to complete the function helperFunction that does not take any argument. This function expects an object of Hello as a return type. Hello is an interface that has the mem
🌐
Kotlin
kotlinlang.org › docs › lambdas.html
Higher-order functions and lambdas | Kotlin Documentation
It's very common for a lambda expression to have only one parameter. If the compiler can parse the signature without any parameters, the parameter does not need to be declared and -> can be omitted.
🌐
javathinking
javathinking.com › blog › writing-a-lambda-expression-when-parameters-are-ignored-in-the-body
How to Write a Lambda Expression When Parameters Are Ignored in the Body: Avoiding Compilation Errors & Best Practices — javathinking.com
A lambda expression is a short block of code representing an anonymous function. It has a parameter list, an arrow operator (->), and a body. The parameter list must match the signature of the functional interface it implements (a functional interface has exactly one abstract method). To "ignore" parameters ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lambda-expressions-parameters
Java - Lambda Expressions Parameters - GeeksforGeeks
July 23, 2025 - When there is more than one statement, then these must be enclosed in curly brackets (a code block) and the return type of the anonymous function is the same as the type of the value returned within the code block, or void if nothing is returned. These are for single-line lambda expressions having void return type. Type 1: No Parameter.
🌐
Android Developers
developer.android.com › codelabs › basic-android-kotlin-compose-function-types-and-lambda
Use function types and lambda expressions in Kotlin | Android Developers
September 21, 2023 - Lambdas can be written inline without a variable name. If a function's last parameter is a function type, you can use trailing lambda syntax to move the lambda expression after the last parenthesis when you call a function. Higher-order functions are functions that take other functions as ...