Syntax is:

arguments -> body

where arguments can be either

  • ()

  • a single variable if the type of that variable can be inferred from the context

  • a sequence of variables, with or without types (or since Java 11, with var), in parentheses.
    Examples: (x), (x, y), (int x, int y), (var x, var y) (Java 11+).
    The following are invalid: (int x, y), (x, var y), (var x, int y)

and body can be either an expression or a {...} block with statements. The expression (other than a method or constructor call) is simply returned, i.e. () -> 2 is equivalent to () -> {return 2;}


In case of lambda expressions like () -> f() (the body is a method or constructor call expression):

  • if f() returns void, they are equivalent to () -> { f(); }

  • otherwise, they are equivalent to either () -> { f(); } or () -> { return f(); }). The compiler infers it from the calling context, but usually it will prefer the latter.

Therefore, if you have two methods: void handle(Supplier<T>) and void handle(Runnable), then:

  • handle(() -> { return f(); }) and handle(() -> x) will call the first one,

  • handle(() -> { f(); } will call the second one, and

  • handle(() -> f()):

    • if f() returns void or a type that is not convertible to T, then it will call the second one

    • if f() returns a type that is convertible to T, then it will call the first one


The compiler tries to match the type of the lambda to the context. I don't know the exact rules, but the answer to:

What would happen if there were two SwingUtilities.invokeLater methods which differ only in parameter list?

is: it depends on what would be those parameter lists. If the other invokeLater had also exactly one parameter and that parameter would be of type that is also an interface with one method of type void*(), well, then it would complain that it cannot figure out which method you mean.

Why are they written as they are? Well, I think it's because syntax in C# and Scala is almost the same (they use => rather than ->).

Answer from Karol S on Stack Overflow
🌐
Amazon Web Services
docs.aws.amazon.com › aws lambda › developer guide › aws lambda functions › programming languages › building lambda functions with java › java sample applications for aws lambda
Java sample applications for AWS Lambda - AWS Lambda
The GitHub repository for this guide provides sample applications that demonstrate the use of Java in AWS Lambda. Each sample application includes scripts for easy deployment and cleanup, an CloudFormation template, and supporting resources. ... – A Java function that demonstrates how you ...
🌐
W3Schools
w3schools.com › java › java_lambda.asp
Java Lambda Expressions
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 package) used with lists.
Discussions

What is the breakdown for Java's lambda syntax? - Stack Overflow
Please explain the syntax for Java 8's lambda methods. There are a lot of explanations out there for what lambda functions are, but I can't find a thorough explanation of the syntax, and I am find... More on stackoverflow.com
🌐 stackoverflow.com
Any tips for understanding and practicing Java lambda expressions?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
12
1
August 5, 2025
Lambda Expressions, how do they work?
A fine article although most Java devs don't need to know how it works. Like most such articles however it doesn't go into the question of why the lambda infrastructure is so complicated. Compare it to how Kotlin compiles lambdas: it just emits a classfile for an inner object. No invokedynamic, lambda metafactories, runtime bytecode spinning etc. There are a few reasons I believe the Java guys went with this approach, despite its apparent complexity, although this list is 'reverse engineered' from random discussions observed over the years rather than true first hand knowledge: A belief that it's faster to spin bytecode in memory than load it from disk. This may well be true, I don't know. A desire to reduce the number of classfiles in applications. Indeed the Java approach shrinks programs and probably speeds up compilation, as the code is generated by the JVM at runtime instead of by the compiler at compile time. Flexibility to change how lambda classes are created for future optimisations. The latter point is worth exploring more. It's rather theoretical today because despite the age of Java 8, the optimisations put in place for lambdas were more about reducing the overhead of the infrastructure rather than genuinely exceeding what a regular inner class would get. However there are some optimisations that are relevant. One is using anonymous classes. It means the class can be loaded without contending on an internal JVM lock, so when running lots of newly loaded classes from multiple threads simultaneously e.g. during startup, this can avoid lock contention and improve throughput. Another is only generating support for serialisation when it's needed. Another would be " isolated methods ", but that one was never implemented and may not apply to many lambdas. More on reddit.com
🌐 r/java
44
152
October 3, 2020
Are Java 8 features like lambda's encouraged in your company?
I don't want to do pre-8 projects anymore. They can pry my lambda's and my streams from my cold dead hands! More on reddit.com
🌐 r/java
137
72
June 24, 2017
🌐
GeeksforGeeks
geeksforgeeks.org › java › lambda-expressions-java-8
Java Lambda Expressions - GeeksforGeeks
Lambda with a Single Parameter is a lambda expression that accepts one input value. It is used when the operation needs a single argument to produce a result or perform a task. It is not mandatory to use parentheses if the type of that variable can be inferred from the context. Parentheses are optional if the compiler can infer the parameter type from the functional interface. ... import java.util.ArrayList; public class GFG{ public static void main(String[] args){ ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); System.out.println("All elements:"); list.forEach(n -> System.out.println(n)); System.out.println("Even elements:"); list.forEach(n -> { if (n % 2 == 0) System.out.println(n); }); } }
Published: August 29, 2026
🌐
Medium
devcookies.medium.com › a-complete-guide-to-lambda-expressions-in-java-0aea2e1cea42
A Complete Guide to Lambda Expressions in Java
December 3, 2024 - Lambda expressions were introduced in Java 8 to enable functional programming and simplify the verbosity of anonymous classes. They allow you to write concise, functional-style code that is both readable and expressive. This guide will take you through the concepts, syntax, use cases, and practical examples of lambda expressions.
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Serverless-AWS-Lambda-example-in-Java
Create your first Java AWS Lambda function in minutes
Here’s how it works: The Lambda environment passes any payload data to the alterPayload method of the Java class though the payload variable. The println statement logs data to the AWS environment.
Published: July 3, 2025
Find elsewhere
🌐
SentinelOne
sentinelone.com › blog › data platform › aws lambda with java: a simple introduction with examples
AWS Lambda With Java: A Simple Introduction With Examples | Scalyr
October 27, 2022 - Or you can type “lambda” in the search bar. Once you have opened the Lamba Management console, you’ll need to create a function. Choose the following options and create your first function. The runtime needs to be Java 8 or you won’t be able to run Java.
🌐
Baeldung
baeldung.com › home › cloud › a basic aws lambda example with java
A Basic AWS Lambda Example With Java |Baeldung
April 17, 2025 - We specify Java 21 as the runtime, ... ... awslocal lambda invoke \ --function-name baeldung-lambda-function \ --payload '{ "name": "John Doe", "question": "How do I view articles ad-free and in dark mode ......
Top answer
1 of 4
65

Syntax is:

arguments -> body

where arguments can be either

  • ()

  • a single variable if the type of that variable can be inferred from the context

  • a sequence of variables, with or without types (or since Java 11, with var), in parentheses.
    Examples: (x), (x, y), (int x, int y), (var x, var y) (Java 11+).
    The following are invalid: (int x, y), (x, var y), (var x, int y)

and body can be either an expression or a {...} block with statements. The expression (other than a method or constructor call) is simply returned, i.e. () -> 2 is equivalent to () -> {return 2;}


In case of lambda expressions like () -> f() (the body is a method or constructor call expression):

  • if f() returns void, they are equivalent to () -> { f(); }

  • otherwise, they are equivalent to either () -> { f(); } or () -> { return f(); }). The compiler infers it from the calling context, but usually it will prefer the latter.

Therefore, if you have two methods: void handle(Supplier<T>) and void handle(Runnable), then:

  • handle(() -> { return f(); }) and handle(() -> x) will call the first one,

  • handle(() -> { f(); } will call the second one, and

  • handle(() -> f()):

    • if f() returns void or a type that is not convertible to T, then it will call the second one

    • if f() returns a type that is convertible to T, then it will call the first one


The compiler tries to match the type of the lambda to the context. I don't know the exact rules, but the answer to:

What would happen if there were two SwingUtilities.invokeLater methods which differ only in parameter list?

is: it depends on what would be those parameter lists. If the other invokeLater had also exactly one parameter and that parameter would be of type that is also an interface with one method of type void*(), well, then it would complain that it cannot figure out which method you mean.

Why are they written as they are? Well, I think it's because syntax in C# and Scala is almost the same (they use => rather than ->).

2 of 4
13

The syntax is

(parameter_list_here) -> { stuff_to_do; }

The curly braces can be omitted if it's a single expression. The regular parentheses around the parameter list can be omitted if it's a single parameter.

The syntax only works for all functional interfaces. The @FunctionalInterface annotation tells the compiler that you intend to write such an interface and gives a compile error if you do not meet the requirement(s) - for example it must only have 1 overrideable method.

@FunctionalInterface
interface TestInterface {
    void dostuff();
}

Runnable is also declared like that. Other interfaces are not, and they cannot be used with lambda functions.

Now that we've made a new functional interface with a method that takes no parameters, how about we test the question you had about "collision" in the signatures?

public class Main {
    private void test(Runnable r) {

    }
    private void test(TestInterface ti) {

    }
    public static void main(String[] args) { 
        test(() -> { System.out.println("test");})
    }

    @FunctionalInterface
    interface TestInterface {
        void dostuff();
    }
}

Result: compile error: ambigouous call to method test.

You see, the compiler/VM(if done runtime) finds the appropriate methods and their parameter list and sees if the parameter is a functional interface and if it is it creates an anonymous implementation of that interface. Technically (in byte code) it's different from an anonymous class, but otherwise identical (you won't see Main$1.class files).

Your example code (courtesy of Netbeans) can also be replaced with

SwingUtilities.invokeLater(MainAppJFrame::new);

Btw. :)

🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-lambdas-in-java
How to Use Lambdas in Java | DigitalOcean
In this tutorial, you will learn to write your lambda expressions. You will also learn to use some built-in Lambdas available in the java.util.function package.
🌐
Baeldung
baeldung.com › home › java › core 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.
🌐
LinkedIn
linkedin.com › pulse › top-5-lambda-functions-use-injava-eidan-khan-6jo5e
Top 5 Lambda Functions to Use in Java
Login to LinkedIn to keep in touch with people you know, share ideas, and build your career.
🌐
DevGenius
blog.devgenius.io › java-lambda-expressions-how-they-work-with-practical-examples-and-humor-f6d823cd1e40
Java Lambda Expressions: How They Work, With Practical Examples and Humor | by M Business Solutions | Dev Genius
December 9, 2024 - Lambda expressions were introduced in Java 8, and they’ve transformed the way we write Java code. But don’t let the fancy name intimidate you! In this blog, we’ll break down what lambdas are, how they work, and look at some practical examples to help you understand why they’re so useful. Of course, we’ll throw in some humor along the way to keep it fun! In layman’s terms, a lambda expression is just a shortcut for writing anonymous functions (methods without names).
🌐
Blogger
javarevisited.blogspot.com › 2014 › 02 › 10-example-of-lambda-expressions-in-java8.html
10 Example of Lambda Expressions and Streams in Java 8
September 21, 2021 - One of them is Predicate, By using java.util.function.Predicate functional interface and lambda expressions, you can provide logic to API methods to add a lot of dynamic behavior in less code. The following examples of Predicate in Java 8 show a lot of common ways to filter Collection data in Java code.
🌐
Medium
medium.com › simform-engineering › efficient-java-lambdas-and-streams-3c59aff1217d
fficient Java Lambdas and Streams | Simform Engineering
July 5, 2023 - Introduced in Java 8, lambda expressions facilitate functional programming and improve language expressiveness. The syntax typically includes parameters, an arrow token, and a body, following a general format. ... Parameters: These are the inputs to the lambda expression. They can be optional if the lambda expression doesn’t require any inputs. For example, the below syntax represents a lambda expression with no parameters.
🌐
Medium
medium.com › @mehar.chand.cloud › getting-started-with-java-functional-interfaces-simplifying-your-code-with-lambda-expressions-c3181d1e5fc3
Getting Started with Java Functional Interfaces: Simplifying Your Code with Lambda Expressions | by Mehar Chand | Medium
May 14, 2024 - Functional interfaces are the backbone of functional programming in Java. Think of them as special interfaces with just one job: to declare a single method. While they can have other methods, they must have only one abstract method to be functional. This unique feature enables the use of lambda expressions, which are a compact way to create anonymous functions in Java.
🌐
Medium
medium.com › @gaddamnaveen192 › 50-java-8-lambda-snippets-every-developer-should-know-00c4bf90e367
50 Java 8 Lambda Snippets Every Developer Should Know | by Gaddam.Naveen | Medium
June 4, 2025 - List<String> list = Arrays.asList("Java", "Spring", "Lambda"); list.forEach(item -> System.out.println(item)); List<String> list = Arrays.asList("Java", "JavaScript", "Python"); List<String> filteredList = list.stream() .filter(s -> ...
🌐
Medium
medium.com › tuanhdotnet › what-is-a-lambda-function-in-java-and-example-d0b70dbf724f
What Is a Lambda Function in Java and Example. | by Anh Trần Tuấn | tuanhdotnet | Medium
September 19, 2024 - ... To use lambda expressions, you need a functional interface. A functional interface is an interface that has exactly one abstract method. Examples include Runnable, Callable, and custom interfaces with a single abstract method.
🌐
Medium
medium.com › @lakshyachampion › mastering-functional-interfaces-and-lambda-expressions-in-java-c02d8aaff5b2
Mastering Functional Interfaces and Lambda Expressions in Java | by Lakshya Agarwal | Medium
January 17, 2024 - To facilitate this type of lambda, an interface with a single abstract method taking one parameter and returning a result is provided by the java i.e. Function<T, R> interface. In the above example, you have seen that we have not written the return statement, we have just written text.length() with no return statement b/c when a lambda expression has a single expression in its body, and that expression is a value or a method call, you can omit the return statement.
🌐
Medium
medium.com › javarevisited › lambda-functions-in-java-a-step-by-step-guide-72c4aa1778e0
Lambda Functions in Java: A Step-by-Step Guide | by Sumit Kumar Singh | Javarevisited | Medium
June 1, 2026 - Before Lambda functions, if you wanted to pass a piece of code as a parameter to a method, you had to use anonymous inner classes. This made the code long and harder to read, even for simple tasks. ... import java.util.*; public class PreLambdaExample { public static void main(String[] args) { List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David"); Collections.sort(names, new Comparator<String>() { @Override public int compare(String s1, String s2) {…