Java8 function Interface/LAMBDA expression/interface default method/interface static method/interface conflict method override/LAMBDA expression specify generic type, etc.

Source: Internet
Author: User
Tags instance method

One: Functional interface

1. The concept of a functional interface is that this interface must have only one abstract method, which can be displayed by @functionalinterface (similar to @override), but an interface with no such annotations but only one abstract method is also a functional interface ; (Interfaces also have package access permissions as classes, but internal methods are public by default)

@FunctionalInterface  Public Interface  ifoo{    void  print ();} is one of the simplest functional interfaces, but if there is another abstract method such as void Print2 (), this interface is not a functional interface and will be error-@functionalinterface at compile time.

2. There can be static methods in the interface (new features of JDK8)

@FunctionalInterface  Public Interface ifoo{    void  print ();     Static void stest () {      System.out.println ("msg");    }      }

Note that the static method in the interface is default and only public, just like the field in the interface is default and can only be public static; Call can be directly ifoo.stest ();

3. The default method can also be found in the interface (JDK8 new feature)

@FunctionalInterface  Public Interface ifoo{    void  print ();     default void stest () {      System.out.println ("msg");    }      }

Unlike static methods, the default method is essentially an instance method, so it is still necessary to create an IFoo implementation class object to call this method, such as (new IFoo () {...}). Stest (); Yes, but ifoo.stest () is wrong

4. When an interface becomes a functional interface and is used as a functional interface, it is similar to a delegate in C #, where the interface and its abstract methods are one by one corresponding, so the generic declaration is in the interface name rather than the abstract method

@FunctionalInterface  Public Interface extends serializable>{    t print (t arg);   // written in <T> t print (t), for IFoo as a delegate, note <t extends b> t print. It is also available in the normal interface;    staticvoid  stest () {      System.out.println ("msg");    }      }

5. Relationship between a functional interface object and its lambda expression

A lambda expression does not produce a. class file of the inside classes, such as new predicate () {...} is generated, so if you need to convert a lambda expression to an interface object, it needs to be cast, such as: ((predicate) O- false). and ( false)

The above (O-to-false) is a "function object", and the new predicate () {@Override test ...} is not the same, the former does not produce a predicate implementation class, so a forced type conversion is required to convert the result of the lambda expression into an interface implementation class object;

6. Use a functional interface as a delegate

 Public Interface Println<t>{    void  Println (T msg);}

can println<string> println = System.out::p rintln, to assign value;

7. Implementing multiple interfaces but the default method with the same name must override/redefine the method (if it is an abstract method does not matter, when the sub-class implementation of the IC implementation of print so that is actually the implementation of IA is also an IB does not exist to rewrite two times the argument)

 Public Interfaceia{default voidprint () {}} Public Interfaceib{default voidprint () {}} Public InterfaceIcextendsIA, ib{@Overridedefault voidPrint () {IA.Super. print ();}//TODO is used to explicitly specify print with IA, note that prior to JDK8, because there is no default and only one class can be inherited, only super.print () is required .}

Type deduction for 8.LAMBDA expressions

If the non-generic nature does not need, but if the argument is generic, but can not be implicitly deduced when it is possible to pass (String s)->{...} To specify the type of the generic type;

9. The interface can be the default method, when this interface can be directly new itest{} can produce objects, but can also override the default method

10. The default method of the parent interface can be overridden in a sub-interface to make it an abstract method in the subinterface;

The difference between a 11.LAMBDA expression and an anonymous inner class

A lambda expression is a very special existence, it does not generate a. class file, and it is not an object of the implementation class of the interface, so it should be understood that ifoo foo = ()->true; is ifoo to match the lambda expression on the right, Instead of saying that the regular expression is a IFoo object of an anonymous implementation class;

Note that there is an implicit conversion of IFoo foo = (IFoo) ()->true;

12. Store the method in the "delegate"

ITest del = system::getenv; At this time, the abstract method of ITest is realized by System.getenv method.

Del.method ("string"); note that here are a few points, 1) is that itest must be a functional interface; 2) the abstract method name in ITest is arbitrary and can have generics, but must match system.getenv (..) The return value and the parameter list for this method; 3) Del and C # delegate variable or something different. Not directly del (..) To invoke, but to Del.method (..) To invoke the System.getenv method;

N) can also be used itest t = string::new; means the construction method of String; then T.func (..) Method is the new String (..) (The construction method can be understood to some extent as a static method of a Class)

N)ifoo2<string> ss = sbs::concat;, SBS is an object, so Ss.func (..) The actual is to call Sbs.concat (..) Method

N) for if the method test is an instance method of class A, then ITest del = A::test; is wrong, the IDE hints that there is no static test method, because when Del.func (..) Call will not know which object to invoke the test method, because the call of test must depend on a certain object;

Java8 function Interface/LAMBDA expression/interface default method/interface static method/interface conflict method override/LAMBDA expression specify generic type, etc.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.