1. Introduction
Read a few examples of the introduction of JAVA8, which introduced many long-awaited features (although I have not experienced it), but whether your code is a regular youth or literary youth, you can be java8 from the re-organized code to see her simplicity, I had to make some records of the fresh meat from these new (Java) circles so that I could make good use of them.
One of the biggest features is the introduction of the concept of "functional" programming (all of which is plagiarism), and so many novel words, such as "Grammar sugar", "closure", "explicit | implicit function", etc. here I will not one to experience, here is the first contact, first understanding will be used primarily, Details of the depth to be slowly pondering later.
2. Function interface--@Functionnalinterface
Before we get to the code sample, we now have a sense of this functional interface:
- Interfaces must have only one abstract method;
- The interface can have other default or static methods;
- The interface inherits the object class by default, so the method in object cannot be overridden in the interface;
Note: This annotation is not necessary, if an interface satisfies the "function interface" feature, then no comment is not affected, plus the note can better let the compiler check, if the non-conformance specification will be reported compilation error.
For example, in the JDK8 in the comparator, callable, runnable and other interfaces added to the note, the following two code is the same effect:
1 Public Static voidRunthreadbylambda () {2Runnable Runnable = (), System.out.println ("This is a thread implemented with lambda");3 NewThread (runnable). Start ();4 }5 6 //---------------------------------------------------------------------------------7 Public Static voidRunthreadbyinnerclass () {8Runnable Runnable =NewRunnable () {9 @OverrideTen Public voidrun () { OneSystem.out.println ("This is a thread implemented with an internal class"); A } - }; - NewThread (runnable). Start (); the}
Java8 Simple Introduction-Functional interface @FunctionalInterface