Home>Article>Java> Matching method of Lambda expression in Java interface

Matching method of Lambda expression in Java interface

WBOY
WBOY forward
2023-05-04 12:55:06 1638browse

1. Description

(1) Each lambda can match a given type through a specific interface. A so-called functional interface must have one and only one abstract method declaration. Each lambda expression corresponding to it must match the declaration of the abstract method. Since default methods are not abstract, feel free to add default methods to your functional interface.

(2) Any interface containing only one abstract method can be used as a lambda expression. In order for the defined interface to meet the requirements, the interface should be marked with @FunctionalInterface. If a second abstract method is defined in the interface, the compiler will notice this annotation and throw an exception.

2. Example

@FunctionalInterface interface Converter { T convert(F from); } Converter converter = (from) -> Integer.valueOf(from); Integer converted = converter.convert("123"); System.out.println(converted); // 123

Note that if you do not write the @FunctionalInterface annotation, the program is also correct.

What are the characteristics of Java

1. As a representative of static object-oriented programming language, Java language implements object-oriented theory and allows programmers to perform complex programming with an elegant way of thinking.

2.Java has the characteristics of simplicity, object-oriented, distributed, security, platform independence and portability, and dynamic nature.

3. Use Java to write desktop applications, Web applications, distributed systems and embedded system applications, etc.

The above is the detailed content of Matching method of Lambda expression in Java interface. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete