Java8 new Features

Source: Internet
Author: User

Today morning run to challenge the ego, and then the knee pain can not walk, and then I suddenly thought of java11 to come out, I java8 new Xindu did not learn, the most critical is now working with or 6 ... It seems irrelevant, but it's linked together and decided to take the day off and learn.

Method in the interface:

This is the interface

  

This is the implementation class and does not override any method

  

There is no static modified method B and Variable C, only the default modified method a

  

Multiple implementations:

If a class implements more than one interface, these interfaces have an identical method of default void A (), and the implementation class can pass: The method of the interface. Super. method to specify which interface to invoke

  

Lambda expression:

Description (copy of someone else's):
(parameter 1, parameter 2 ...) Indicates that the argument list;-> represents a connector; {} Internally is the method body
1. = The right type is automatically inferred from the functional interface type on the left;
2, if the formal parameter list is empty, just keep ();
3, if the formal parameters only 1, () can be omitted, only the name of the parameter can be;
4, if the execution of the statement only 1 sentences, and no return value, {} can be omitted, if there is a return value, if you want to omit {}, you must also omit the return, and the execution of the statement is guaranteed only 1 sentences;
5. The data type of the formal parameter list is automatically inferred;
6, Lambda does not generate a separate internal class file;
7, lambda expression if the local variable is accessed, the local variable must be final, if the local variable is not added to the final keyword, the system will be automatically added, and then modify the local variable, will be error;

Let's start with a simple one:

Define an interface

    

And then another class, the class has a variable I, a value of 12, with the normal new one interface way:

    

Then look at the way lambda expressions are written, where the console does not print AAA, because only the method body and parameters are assigned to the interface, but the method in the interface is not called

    

Well, a bit like JS, a statement is then assigned to a variable: var a = $ ("a"). Val () ...

Then there is the parameter:

This is an interface

    

This is the method in the class, here is the output AAAAA:

    

Explain here, because the lambda expression can only be used for the function interface, that is, there is only one method without the method of the interface, the code of a means the variable name of the method parameter, type expression automatic judgment, and the following output statement represents the method body, because there is only one so directly omitted {}. The end is returned to the interface object. The purpose of this code is to output the value passed in. So the last interface object calls the appropriate method to output the printed value to the result. Yes, here is the method body and the variable name, and then return to the interface object, finally call method execution method body ...

You can also do this by adding a method to the calling interface in the class

    

Then call this method:

    

Then there are multiple parameters:

Interface

    

expression, it is found that the parameter name must be inside the parentheses, otherwise the error is:

    

or write this:

    

Test it:

    

There's no return value, and then there's the return value.

Here's a simple one:

    

Then you use an expression:

    

It is shown that the return is gray, the representative can be omitted, the most said, return omitted when {} must also be omitted:

    

The next is how miserable, fast, a parameter is saved:

    

Class:

    

Then comes the Java-brought expression:

Determine whether the passed in string exists in the specified string, not just Java, I do not know in this test project which comes from a Google package, there is also a lambda expression of this function:

    

Not to be continued ...

    

Java8 new Features

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.