Sitemap

Lambda Expressions

3 min readJul 27, 2025

--

Press enter or click to view image in full size

Method vs Lambda

Method is always belongs to any class or object.

Lambda expressions does not belong to any class or object.

Method has — name, parameter list, body, return type.

Lambda expression

A lambda expression is a short block of code that takes in parameters and returns a value, used to implement the single abstract method of a functional interface. It enables you to treat functionality as a method argument or assign it to a variable.

It is an anonymous function so no need to have a name. A lambda expression in Java is a concise way to represent an anonymous function — that is, a method without a name that you can pass around as a value.

It has a parameter list and method body. [body can have single or multiple statements]

No need to have a return type. Java 8 compiler can infer the return type.

Get hansini gunasena’s stories in your inbox

Join Medium for free to get updates from this writer.

Lambda expression syntax

This function take two integer parameters and returns the sum.

🔹 Why Use Anonymous Functions?

An anonymous function is a function without a name. You define it inline, often to pass as an argument to another method or to assign to a variable.

In Java, lambda expressions are anonymous functions.

1. Concise code
No need to create a full method or class for small logic.

2. Functional programming
Lets you
treat behavior as data, pass functions around.

3. Readability + Reusability
Useful for
one-time use logic like filtering, sorting, etc.

An anonymous function is a nameless function used to define behavior inline. In Java, it’s implemented using lambda expressions and is useful for passing behavior to methods like filter(), sort(), or map() — leading to cleaner, shorter, and more expressive code.

Implementing Functional interfaces method through lambda expressions

--

--