📘 Lesson  ·  Lesson 43

Lambda Expressions

Lambda Expressions

What is a Lambda?

A lambda expression is a short way to write a function with no name. Syntax: (params) -> { body }.

Example

// Old way
Runnable r1 = new Runnable() {
    public void run() { System.out.println("Hi"); }
};

// Lambda way (shorter)
Runnable r2 = () -> System.out.println("Hi");
r2.run();

// With parameters
interface Calc { int op(int a, int b); }
Calc add = (a, b) -> a + b;
System.out.println(add.op(5, 3));   // 8
Hi 8

Summary

  • Lambda = short anonymous function: (params) -> body.
  • Used with functional interfaces (one abstract method).

Lambda क्या है?

Lambda expression बिना नाम के function लिखने का छोटा तरीका है। Syntax: (params) -> { body }

Example

// पुराना तरीका
Runnable r1 = new Runnable() {
    public void run() { System.out.println("Hi"); }
};

// Lambda तरीका (छोटा)
Runnable r2 = () -> System.out.println("Hi");
r2.run();

// parameters के साथ
interface Calc { int op(int a, int b); }
Calc add = (a, b) -> a + b;
System.out.println(add.op(5, 3));   // 8
Hi 8

सारांश

  • Lambda = छोटा anonymous function: (params) -> body
  • Functional interfaces (एक abstract method) के साथ use होता है।
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n