Home >Java >javaTutorial >Introduction to lambda expressions

Introduction to lambda expressions

DDD
DDDOriginal
2025-01-07 08:08:40579browse

Introdução às expressões lambda

  • Introduced in JDK 8.

  • Increased the power of expression of the Java language.

  • Added new syntax elements and optimized common structures.

  • Comparison: Just as generics reshaped Java in the past, lambda expressions are reshaping it today.

  • Lambda expressions introduce a more concise and expressive style of programming.

  • Promote more functional programming.

Lambda Expression:

  • Anonymous method (without name).

  • It is not executed in isolation; implements methods of a functional interface.

  • Equivalent to a form of anonymous class.

  • Lambdas also known as closures (capture variables from their scope).

Functional Interface:

  • Contains only one abstract method.

  • Represents a single action or purpose.

  • Example: Runnable with the run() method.

  • Defines the target type of a lambda expression.

  • Also called SAM (Single Abstract Method) type.

Rules and Observations

  • A lambda expression can only be used in contexts with a specified target type.

  • The target type is the expected type of the lambda expression, and it needs to be compatible with the type of the abstract method of the functional interface

  • Functional interfaces can include public Object methods (such as equals()) without losing their functional interface status. These methods are considered implicitly implemented.

The above is the detailed content of Introduction to lambda expressions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn