Java8 Simple Introduction-Functional interface @FunctionalInterface

Source: Internet
Author: User

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:

    1. Interfaces must have only one abstract method;
    2. The interface can have other default or static methods;
    3. 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

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.