> ## Content Index
> Fetch the complete content index at: https://www.javahandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How To Write Lambda Expressions
- URL: https://www.javahandbook.com/java-streams-api/how-to-write-lambda-expressions/
- Published: 2026-02-15T09:24:10.000Z
- Updated: 2026-02-15T09:24:10.000Z
- Description: This lesson introduces the basic lambda expression structure with tips to write efficient code.
- Author: Gopi Gorantala
- Tags: #docs, #java-streams-api, Lambda Expressions

First, let us look at the basic structure of lambda expression. This could help us understand writing lambdas easily.

## Basic Structure

The basic structure of lambda expression is:

![](https://storage.ghost.io/c/00/01/0001b1c8-8c52-4cdb-b3c7-c0746bea790e/content/images/2026/02/image-7.png)

## Basic steps

Java Lambda expression contains the following parts:

**Parameters-list:** This can be empty or contain one or more parameters, separated by commas. If there is only one parameter, you can omit the parentheses.

1. **Lambda operator:** This separates the parameter list from the lambda expression's body or, in simple terms, connects the argument list with the lambda expression's body.
2. **Expression:** Finish with the expression or statement(s) to be executed: `expression` or `{ statements }`.

## Example

Here is an example of a lambda expression that takes two integers as input and returns their sum:

```java
(int a, int b) -> a + b
```

This lambda expression takes two integer parameters `a` and `b` and returns their sum. Note that the parameter types are specified in parentheses, and the return type is inferred based on the expression.

```java
() -> System.out.println("Hello, world!")
```

This lambda expression has an empty parameter list, and the body contains a single statement to print a message to the console.

Lambda expressions can be assigned to functional interfaces or used directly as arguments to methods. For example, to use the above lambda expression with the `Runnable` functional interface, you can write:

```java
Runnable helloWorld = () -> System.out.println("Hello, world!");
```

This creates a `Runnable` object that executes the lambda expression when its `run` method is called.

## Tips when writing lambda expressions

Parameter lists sometimes have zero or more parameters.

There are rules to follow when writing a lambda expression. We don’t need to follow all, but to improve code quality we have to follow these tips when writing a lambda expression.

1. When there are no parameters, we should explicitly give `()`.

If there is a single parameter, we can ignore the parenthesis below. Both syntaxes are valid.

- `(message) -> {}`
- `message -> {}`
- When the body is a single line, we can exclude the curly braces `{`, `}` and `return`. The following example explains it.

```java
// Both of the below expressions are valid.

// 01: Lambda expression printing Hello on console
  () -> {
    System.out.println("Hello");
  }

// 02: Lambda expression printing Hello on console
  () -> System.out.println("Hello");
```

- Java understands the underlined functional interface when running through the lambda expression. Because of this, we don’t need to have the parameter typed inside the parameters list.

```java
// All of the below syntaxes print same output - Hello, fullName
(String fullName) -> {
    System.out.println("Hello, " + fullName);
}

// Above can be refactored as follows.
(fullName) -> {
    System.out.println("Hello, " + fullName);
}

// further more, we can replace it with
fullName -> System.out.println("Hello, " + fullName);
```