New features in Java 8

Source: Internet
Author: User
Tags stream api

New features in Java 8

Java 8 (also known as JDK 1.8) is a major version of Java language development. Oracle Corporation released Java 8 on March 18, 2014, which supports functional programming, new JavaScript engines, new date APIs, new stream APIs, and more.

New features

Java8 has added a lot of features, we mainly discuss the following:

    • The Lambda expression −lambda allows the function to be passed as a parameter of a method (function as an argument into the method).

    • method Reference − A method reference provides a very useful syntax to directly reference a method or constructor that already has a Java class or object (instance). Used in conjunction with lambda, method references can make language constructs more compact and concise, reducing redundant code.

    • default Method − The default method is a method that has an implementation in the interface.

    • new Tools − New compiler tools, such as: Nashorn engine JJs, class-dependent parser jdeps.

    • Stream API − The newly added stream API (Java.util.stream) introduces a true functional programming style into Java.

    • Date Time API − strengthens the processing of dates and times.

    • The Optional class −optional class has become part of the Java 8 class library To resolve null pointer exceptions.

    • Nashorn, JavaScript engine −java 8 provides a new Nashorn JavaScript engine that allows us to run specific JavaScript applications on the JVM.

Programming style

Java 8 wants its own programming style and differs from Java 7, and the following examples show the programming formats for Java 7 and Java 8:

Java8tester.java File Code:Import Java.Util.Collections;Import Java.Util.List;Import Java.Util.ArrayList;Import Java.Util.Comparator;Public Class Java8tester { Public Static void Main(String Args[]){ List<String>Names1=New ArrayList<String>();Names1.Add("Google");Names1.Add("Runoob");Names1.Add("Taobao");Names1.Add("Baidu");Names1.Add("Sina");List<String>Names2=New ArrayList<String>();Names2.Add("Google");Names2.Add("Runoob");Names2.Add("Taobao");Names2.Add("Baidu");Names2.Add("Sina");Java8tester Tester=New Java8tester();System.Out.println("Using Java 7 Syntax:");Tester.SortUsingJava7(Names1);System.Out.println(Names1);System.Out.println("Using Java 8 Syntax:");Tester.SortUsingJava8(Names2);System.Out.println(Names2);} //Sorting using Java 7 Private void SortUsingJava7(List<String>Names){ Collections.Sort(Names,New Comparator<String>() {@Override Public Int Compare(String S1,String S2) { Return S1.CompareTo(S2);} });} //Sorting using Java 8 Private void SortUsingJava8(List<String> names) { collectionssort (names (s1s2) s1< Span class= "Hl-code". compareto (s2 ) } } /span>

New Java 8 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.