Lambda Expressions Syntax - Java vs Kotlin

Both Java and Kotlin support lambda expressions. There are similarities in their syntax, though they are precisely not the same. Many of you may be using both languages at work. When you use multiple languages, the syntax of another language may peek in. In my example, after using Groovy for several years, omitting semicolon (;) was a common mistake while I was programming in Java.

I noticed that while programming with Kotlin, my lambda expression wouldn't compile several times. When I took a closer look at the code, I realised that I was using Java syntax instead.

Let us take a closer look at the differences between Java and Kotlin in their lambda expressions syntax. To start with, let's take a look at the following examples.


Supplier<String> messageProvider = () -> "Hello";
Function<String, String> greet = person -> "Hello " + person;
BiFunction<Integer, Integer, Integer> add = (first, second) -> first + second;


val messageProvider = { "Hello" }
val greet: (String) -> String = { person -> "Hello $person" }
val add: (Int, Int) -> Int = { first, second -> first + second }

In the above code blocks, we have three lambda expressions written in both Java and Kotlin. Since our focus is the syntax of lambda expressions, let us focus on the right-hand side of the expressions (what follows '=' symbol).

The first lambda expression takes no parameters and returns a value. In Java, you had to put a pair of empty parentheses to indicate no parameters. In contrast, in Kotlin, you could have the expression (body), and we can even omit the '->' symbol since we don't have any parameters.

The second lambda expression takes one parameter and returns a value. In Java, parentheses are optional for a single parameter.

The third lambda expression declares two parameters and returns a value. In Java, when you have more than one parameters, enclosing them in parentheses is mandatory.

Note how in Kotlin the entire lambda expression is always enclosed within a pair of brace '{}' symbols.

Now let's take a look at lambdas with multiple statements/ expressions in their bodies. Let's achieve this by simply adding a print statement to the body of add.


BiFunction<Integer, Integer, Integer> add = (first, second) -> {
    return first + second;


val add: (Int, Int) -> Int = { first, second ->
    first + second

In Java, we had to surround the body with the curly braces. Also, we had to add the return statement explicitly. In Kotlin, all we had to do was to add the print statement to the body.

When you move from Java to Kotlin, the curly braces surrounding the entire lambda expression seems like noise. However, this helps in making the experience of evolving the lambda expression very fluent. You can add another parameter or add statements to the body seamlessly.

An overview of the lambda expressions grammar in Java is as follows.

  LambdaParameters -> LambdaBody


For more details, refer

An overview of the lambda expressions grammar in Kotlin is as follows.

    : LCURL NL* statements NL* RCURL
    | LCURL NL* lambdaParameters? NL* ARROW NL* statements NL* RCURL

    : lambdaParameter (NL* COMMA NL* lambdaParameter)* (NL* COMMA)?

    : variableDeclaration
    | multiVariableDeclaration (NL* COLON NL* type)?

For a deeper understanding of the grammar refer to the following links


Once I took the time to explore these details, the mistakes reduced drastically.

Show Comments