Home >Java >javaTutorial >Matching method of Lambda expression in Java interface
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<F, T> { T convert(F from); } Converter<String, Integer> 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.
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!