Introduction to the new syntax features of JDK8

Introduction to the new syntax features of JDK8


JDK8 adds new syntax features:

* Lambda expression

* Functional Interface

* Method Reference

* default method (defender method)

* Bulk data operations


① Lambda expressions

Lambda expressions in Java are based on a single method interface (Single Abstract Method). A single method interface, as the name implies, has only one method in the interface.

Take a chestnut:

In the Runnable interface, there is only one run() method. This Runnalbe interface is a single-method interface.

Lambda expression is an anonymous method. The function in the single method interface (Single Abstract Method) can be treated as a method parameter, which makes the code more concise.

In Java, the return value of a lambda expression is an object, which must be a single method interface (Single Abstract Method) object.


② Functional Interface

An interface that contains only one abstract method, so it is also called an interface of SAM (Single Abstract Method) type. For example, the Runnable interface, which we are familiar with, defines only one abstract method, run(), which is a functional interface. New in Java8 Many functional interfaces have been added. If you want to know more about which functional interfaces have been added, and why so many functional interfaces have been added, you can find the answer from the article.


③ Method Reference

A special form of lambda expression. When the body of a lambda expression only calls a method, in this case, a method reference is used instead of the lambda expression to formally refer to the method directly, which is better than in the lambda expression Citations in the body are more concise in form.


④ default method

The Default method can be considered as a bridge between Lambda expressions and the JDK class library. The main purpose of introducing the Default method is to upgrade the standard JDK interface, and also to allow us to finally use Lambda expressions smoothly in Java8.


⑤ 批量数据操作(bulk dataoperations)

引入批量数据操作的目的是应用lambda函数来实现包含并行操作在内的多种数据处理功能,而支持并行数据操作是其关键内容.这个并行操作是在Java7 java.util.concurrency的Fork/Join机制上实现的.


⑥ 总结

Lambda表达式极大地提升的代码可读性并使Java语言更加具有表现力,尤其当我们使用新增的数据流API时.相应地,default方法对API升级至关重要,它用来把Lambda表达式集成到集合API中,为我们使用提供便利.

所有新特性的终极目标却是引入并行类库和无缝地利用多核硬件的优势.这些新的变化,将使我们能够以更加高效的方式充分利用Java语言.


⑦ 相关文章

Java 8 Revealed: Lambdas, Default Methods and Bulk Data Operations


https://zeroturnaround.com/rebellabs/java-8-revealed-lambdas-default-methods-and-bulk-data-operations/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325859904&siteId=291194637