Java 9 is coming soon. How much do you know about the ten new features of Java 8 ?, Java9java8

Source: Internet
Author: User
Tags stream api

Java 9 is coming soon. How much do you know about the ten new features of Java 8 ?, Java9java8

Java 9 is expected to be released in September. We will wait and see if it will be a milestone version. Today, let's review the ten new features of Java 8 released in 2014.

Java 8 is the most revolutionary version since Java 5. It has brought many new features in terms of languages, compilers, class libraries, development tools, and Java virtual machines. Let's review these features one by one.

1. Lambda expressions

Lambda expressions can be said to be the biggest selling point of Java 8. She introduced functional programming into Java. Lambda allows a function to be used as a method parameter or code as data.

A Lambda expression can be expressed by a comma-separated parameter list, a-> symbol, and a function body. For example:

Arrays. asList ("p", "k", "u", "f", "o", "r", "k "). forEach (e-> System. out. println (e ));

1 Arrays. asList ("p", "k", "u", "f", "o", "r", "k "). forEach (e-> System. out. println (e ));

To make existing functions better support Lambda expressions, Java 8 introduces the concept of functional interfaces. A functional interface is a common interface with only one method. Java. lang. Runnable and java. util. concurrent. Callable are typical examples of functional interfaces. To this end, Java 8 adds a special annotation @ FunctionalInterface:

1 @FunctionalInterface2 public interface Functional {3     void method();4 }

Ii. Default and static Interfaces

We can define the default method in the interface, use the default keyword, and provide the default implementation. All classes that implement this interface will accept the implementation of the default method, unless the subclass provides its own implementation. For example:

1 public interface DefaultFunctionInterface {2     default String defaultFunction() {3         return "default function";4     }5 }

We can also define static methods in the interface, use the static keyword, or provide implementation. For example:

1 public interface StaticFunctionInterface {2     static String staticFunction() {3         return "static function";4     }5 }

The introduction of the default method and static method of the interface can be considered as introducing the abstract class concept in C ++. In the future, we no longer need to write duplicate code in each implementation class.

Iii. Method reference

It is usually used together with Lambda expressions and can directly reference methods of existing Java classes or objects. There are generally four different method references:

  1. Constructor reference. The syntax is Class: new, or a more general Class <T>: new. The constructor method is required to have no parameters;

  2. Static Method reference. The syntax is Class: static_method, which requires a Class parameter;

  3. Any object method reference of a specific class. Its syntax is Class: method. The required method has no parameters;

  4. Method reference of a specific object. Its syntax is instance: method. The method is required to accept a parameter. Different from 3, 3 calls the method on the list element, and 4 calls the method on an object, passing the list element as a parameter;

Iv. Repeated annotations

There is a limit for using annotations in Java 5, that is, the same annotations can only be declared once at the same position. Java 8 introduces repeated annotations so that the same annotations can be declared multiple times in the same place. The Repeatable annotation is required. Java 8 is optimized at the compiler layer, and the same annotation is saved as a set, so the underlying principle remains unchanged.

5. Support for extended annotations

Java 8 extends the annotation context and can add annotations to almost anything, including implementation of local variables, generic classes, parent classes and interfaces. Annotations can also be added to exceptions of method connection.

6. Optional

Java 8 introduces the Optional class to prevent null pointer exceptions. The Optional class is first introduced by Google's Guava project. The Optional class is actually a container: it can save the value of type T or save null. With the Optional class, we do not need to explicitly perform a null pointer check.

VII. Stream

Stream API introduces the real functional programming style to Java. In fact, Stream can be understood as MapReduce. Of course, Google's MapReduce is inspired by functional programming. She is actually a series of elements supporting continuous and parallel aggregation operations. In terms of syntax, it is also similar to the linux pipeline or chain programming. The code is concise and clear, and it is very cool!

8. Date/Time API (JSR 310)

The new Date-Time API (JSR 310) in Java 8 is affected by Joda-Time and provides a new java. time package, which can be used to replace java. util. date and java. util. calendar. Generally, Clock, LocaleDate, LocalTime, LocaleDateTime, ZonedDateTime, and Duration classes are used. The improvements to time and date are very good.

IX. JavaScript Engine Nashorn

Nashorn allows the development and running of JavaScript applications on JVM, and allows Java and JavaScript to call each other.

10. Base64

In Java 8, Base64 encoding has become the standard for Java class libraries. The Base64 class also provides URL-and MIME-friendly encoder and decoder.

In addition to these ten new features, there are also some new features:

  • Better type speculation Mechanism: Java 8 has greatly improved the type speculation, which makes the code more clean and does not require too many forced type conversions.

  • Compiler Optimization: Java 8 adds the parameter name of the method to the bytecode so that the parameter name can be obtained through reflection at runtime. You only need to use the-parameters parameter during compilation.

  • Parallel Array: Supports parallel processing of arrays, mainly the parallelSort () method, which can greatly improve the speed of array sorting on multi-core machines.

  • Concurrency (Concurrency): Based on the newly added Stream Mechanism and Lambda, some new methods are added to support clustering operations.

  • Nashorn engine jjs: Nashorn-based command line tools. It accepts some JavaScript source code as parameters and executes the source code.

  • Class dependency analyzer jdeps: Displays the package-level or class-level dependencies of Java classes.

  • The JVM PermGen space is removed.: Replaced by Metaspace (JEP 122 ).

Java 8 is a huge update, which takes a lot of time for engineers and draws on many other languages and libraries. We cannot list them in detail here. We will have the opportunity to explain them in detail in the future.

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.