New features in Java 8 (i)

Source: Internet
Author: User

Java 8 has been out for a long time, because I have been doing Ruby has no time to study, take advantage of the empty tidy up

Http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html

Here we mainly look at the new features related to programming languages:

Lambda expression:

The problem with anonymous classes now is that when an anonymous class is implemented very simply, such as an interface that contains only an abstract method, its anonymous class implementation is cumbersome and unclear when parsing. In this case, we usually try to pass the implementation of the feature as a parameter of another method, such as when a button is clicked in Swing, the processing action needs to be done. Lambda expressions allow you to do this, either as a method parameter or as a data source. This feature is a feature of functional programming, passing in a block of code as a parameter, and if you have studied Ruby, it should be well understood, similar to Ruby's lambda expression. Let's take a look at the official example (because the example is too long to show only the important parts, and you are interested to go to the address above yourself):

Before using a lambda expression:

Printpersons (Roster, new Checkperson () {Public boolean test (person p) {return p.getgender () = =        Person.Sex.MALE && p.getage () >= && p.getage () <= 25; }    });

After the use:

Printpersons (Roster, (person P), p.getgender () = = Person.Sex.MALE && p.getage () >= 18 && p.getage () <= 25);

you can see the use of lambda after the word needs to give the method implementation of code, concise, code nesting is also less than two layers, if the implementation code has more than one line, you need to use {} at the end of the same time must have return, but also the type of the parameter can be omitted, the Java compiler can automatically derive parameter types based on the method definition.

The default method for the interface:

Java 8 allows you to define a non-abstract method in an interface that requires the keyword default, which is called the method or extension method of the interface, which can define multiple.

The specific code is as follows:

Public interface Parser {string GetResult (string info);    Default String Notknow () {return ' Sorry I do ' t know! ";    } default String Anotherone () {return ' another one '; }}

public class ageparser implements parser {      @ Override    public string getresult (String info)  {         if  (Info.matches ("how old are you")  {             return  "I ' m 23";         } else {             return notknow ();        }    }     public static void main (String[] args)  {         ageparser parser = new ageparser ();         system.out.println (Parser.getresult ("How old are you"));         systEm.out.println (Parser.getresult ("You do ' T know"));         System.out.println (Parser.anotherone ());     }}

The default method can be used directly in the implementation class of the interface without rewriting, of course, it is possible to override this method in the implementation class, and if other interfaces inherit the interface, the default method is inherited, and the default method can be overridden in the interface.

Function-type interface:

In the lambda expression above, it has been suggested that an interface contains only a single abstract method (which can contain multiple default methods), which is called a functional interface, and only a functional interface can use a lambda expression to create an instance of its corresponding anonymous inner class. At the same time, in order to ensure that the interface must meet the requirements of the functional interface, you can add @functionalinterface annotations on the interface declaration, if not meet the requirements, such as the abstract method superfluous one, will compile however.

New features in Java 8 (i)

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.