Home  >  Article  >  Java  >  What are the new features of Java8?

What are the new features of Java8?

巴扎黑
巴扎黑Original
2017-07-24 16:53:351382browse

1. Interface improvement

a. Static methods can be defined in the interface

b. More importantly, the methods in the interface can be modified with default and then add method bodies

2. Why can't we use the default method to override the equals, hashcode, and toString methods?

That is, the interface cannot provide a default implementation of any method of the Object class. If a class implements a method, that always takes precedence over the default implementation. Once all interface instances are subclasses of Object, all interface instances already have non-default implementations of equals/hashCode/toString. Therefore, a default version of these on the interface is useless and will not be compiled.

3. Functional interface

The core concept is functional interface. If an interface defines a single abstract method, then the interface becomes a functional interface. For example, java.lang.Runnable is a functional interface because it only defines one abstract method:

public abstract void run();
 

What is a functional interface? There are two situations: 1. The interface has only one abstract method, abstract modification 2 .The interface has only one abstract method, abstract modification. At the same time, it contains multiple default methods, because the default method is modified by default, not abstract.

At the same time, a new Annotation is introduced: @FunctionalInterface. You can put it in front of an interface to indicate that the interface is a functional interface. Adding an interface to it won't compile unless you manage to turn it into a functional interface. It's a bit like @Override, which declares an intention to use it to prevent you from using it incorrectly.

4.Lambdas 

A very valuable property of functional interfaces is that they can be instantiated with lambdas. Here are some examples of lambdas:

On the left is a comma-separated input list of the specified type, and on the right is a code block with return:

(int x, int y) -> { return x + y; }

On the left is a comma-separated input list of the derived type , the right side is the return value:

(x, y) -> x + y

The left side is a single parameter of the derivation type, the right side is a return value:

x -> x * x

There is no input on the left side (official name: "burger arrow"), and it is returned on the right side A value:

() -> x

The left side is a single parameter of the deduced type, and the right side is a code block with no return value (return void):

x -> { System.out.println(x); }

Static method reference:

String::valueOf

Non Static method reference:

Object::toString

Inherited function reference:

x::toString

Constructor reference:

ArrayList::new

You can come up with some function reference formats that serve as shorthand for other lambda formats.

##Method reference Equivalent lambda expressionString::valueOfx -> String.valueOf(x)Object::toStringx -> x.toString()x::toString() -> x.toString()ArrayList::new () -> new ArrayList()

  当然,在Java里方法能被重载。类可以有多个同名但不同参数的方法。这同样对构造方法有效。ArrayList::new能够指向它的3个构造方法中任何一个。决定使用哪个方法是根据在使用的函数式接口。

  一个lambda和给定的函数式接口在“外型”匹配的时候兼容。通过“外型”,我指向输入、输出的类型和声明检查异常。

给出两个具体有效的例子:

Comparator c = (a, b) -> Integer.compare(a.length(),
                                                 b.length());

一个Comparator的compare方法需要输入两个阐述,然后返回一个int。这和lambda右侧的一致,因此这个任务是有效的。

Runnable r = () -> { System.out.println("Running!"); }

一个Runnable的run方法不需要参数也不会返回值。这和lambda右侧一致,所以任务有效。

在抽象方法的签名里的受检查异常(如果存在)也很重要。如果函数式接口在它的签名里声明了异常,lambda只能抛出受检查异常。

5.捕获和非捕获的Lanbdas表达式 

  当Lambda表达式访问一个定义在Lambda表达式体外的非静态变量或者对象时,这个Lambda表达式称为“捕获的”。比如,下面这个lambda表达式捕捉了变量x:

  int x = 5; return y -> x + y;

  为了保证这个lambda表达式声明是正确的,被它捕获的变量必须是“有效final”的。所以要么它们需要用final修饰符号标记,要么保证它们在赋值后不能被改变。

Lambda表达式是否是捕获的和性能悄然相关。一个非不捕获的lambda通常比捕获的更高效,虽然这一点没有书面的规范说明(据我所知),而且也不能为了程序的正确性指望它做什么,非捕获的lambda只需要计算一次. 然后每次使用到它都会返回一个唯一的实例。而捕获的lambda表达式每次使用时都需要重新计算一次,而且从目前实现来看,它很像实例化一个匿名内部类的实例。

6.其他

  lambdas不做的事

你应该记住,有一些lambdas不提供的特性。为了Java 8它们被考虑到了,但是没有被包括进去,由于简化以及时间限制的原因。

Non-final* 变量捕获 - 如果一个变量被赋予新的数值,它将不能被用于lambda之中。"final"关键字不是必需的,但变量必须是“有效final”的(前面讨论过)。这个代码不会被编译:

int count = 0;
List strings = Arrays.asList("a", "b", "c");
strings.forEach(s -> {
    count++; // error: can't modify the value of count });

例外的透明度 - 如果一个已检测的例外可能从lambda内部抛出,功能性的接口也必须声明已检测例外可以被抛出。这种例外不会散布到其包含的方法。这个代码不会被编译:

void appendAll(Iterable values, Appendable out) throws IOException { // doesn't help with the error values.forEach(s -> {out.append(s); // error: can't throw IOException here // Consumer.accept(T) doesn't allow it });}

有绕过这个的办法,你能定义自己的功能性接口,扩展Consumer的同时通过像RuntimeException之类抛出 IOException。我试图用代码写出来,但发现它令人困惑是否值得。

控制流程 (break, early return) -在上面的 forEach例子中,传统的继续方式有可能通过在lambda之内放置 "return;"来实现。但是,没有办法中断循环或者从lambda中通过包含方法的结果返回一个数值。例如:

final String secret = "foo"; boolean containsSecret(Iterable values) {
    values.forEach(s -> { if (secret.equals(s)) {
            ??? // want to end the loop and return true, but can't }});
}

进一步阅读关于这些问题的资料,看看这篇Brian Goetz写的说明:在 Block中响应“已验证例外”What are the new features of Java8?

 
其它翻译版本(1)

为什么抽象类不能通过利用lambda实例化

抽象类,哪怕只声明了一个抽象方法,也不能使用lambda来实例化。

下面有两个类 Ordering 和 CacheLoader的例子,都带有一个抽象方法,摘自于Guava 库。那岂不是很高兴能够声明它们的实例,像这样使用lambda表达式?

Ordering order = (a, b) -> ...;

CacheLoader loader = (key) -> ...;

这样做引发的最常见的争论就是会增加阅读lambda的难度。以这种方式实例化一段抽象类将导致隐藏代码的执行:抽象类的构造方法。

另一个原因是,它抛出了lambda表达式可能的优化。在未来,它可能是这种情况,lambda表达式都不会计算到对象实例。放任用户用lambda来声明抽象类将妨碍像这样的优化。

外,有一个简单地解决方法。事实上,上述两个摘自Guava 库的实例类已经证明了这种方法。增加工厂方法将lambda转换成实例。

 

Ordering order = Ordering.from((a, b) -> ...);
CacheLoader loader = CacheLoader.from((key) -> ...);

要深入阅读,请参看由 Brian Goetz所做的说明: response to "Allow lambdas to implement abstract classes"。

 

java.util.function

包概要:java.util.function

作为Comparator 和Runnable早期的证明,在JDK中已经定义的接口恰巧作为函数接口而与lambdas表达式兼容。同样方式可以在你自己的代码中定义任何函数接口或第三方库。

但有特定形式的函数接口,且广泛的,通用的,在之前的JD卡中并不存在。大量的接口被添加到新的java.util.function 包中。下面是其中的一些:

  • Function -T作为输入,返回的R作为输出

  • Predicate -T作为输入,返回的boolean值作为输出

  • Consumer - T作为输入,执行某种动作但没有返回值

  • Supplier - 没有任何输入,返回T

  • BinaryOperator -两个T作为输入,返回一个T作为输出,对于“reduce”操作很有用

这些最原始的特征同样存在。他们以int,long和double的方式提供。例如:

  • IntConsumer -以int作为输入,执行某种动作,没有返回值

这里存在性能上的一些原因,主要释在输入或输出的时候避免装箱和拆箱操作。

 
 
 

你应该记住,有一些lambdas不提供的特性。为了Java 8它们被考虑到了,但是没有被包括进去,由于简化以及时间限制的原因。

Non-final* 变量捕获 - 如果一个变量被赋予新的数值,它将不能被用于lambda之中。"final"关键字不是必需的,但变量必须是“有效final”的(前面讨论过)。这个代码不会被编译:

int count = 0;
List strings = Arrays.asList("a", "b", "c");
strings.forEach(s -> {
    count++; // error: can't modify the value of count });

例外的透明度 - 如果一个已检测的例外可能从lambda内部抛出,功能性的接口也必须声明已检测例外可以被抛出。这种例外不会散布到其包含的方法。这个代码不会被编译:

void appendAll(Iterable values, Appendable out) throws IOException { // doesn't help with the error values.forEach(s -> {out.append(s); // error: can't throw IOException here // Consumer.accept(T) doesn't allow it });}

有绕过这个的办法,你能定义自己的功能性接口,扩展Consumer的同时通过像RuntimeException之类抛出 IOException。我试图用代码写出来,但发现它令人困惑是否值得。

控制流程 (break, early return) -在上面的 forEach例子中,传统的继续方式有可能通过在lambda之内放置 "return;"来实现。但是,没有办法中断循环或者从lambda中通过包含方法的结果返回一个数值。例如:

final String secret = "foo"; boolean containsSecret(Iterable values) {
    values.forEach(s -> { if (secret.equals(s)) {
            ??? // want to end the loop and return true, but can't }});
}

进一步阅读关于这些问题的资料,看看这篇Brian Goetz写的说明:在 Block中响应“已验证例外”

What are the new features of Java8?
What are the new features of Java8?
翻译于 4年前
4人顶
 翻译得不错哦!
 
其它翻译版本(1)

为什么抽象类不能通过利用lambda实例化

抽象类,哪怕只声明了一个抽象方法,也不能使用lambda来实例化。

下面有两个类 Ordering 和 CacheLoader的例子,都带有一个抽象方法,摘自于Guava 库。那岂不是很高兴能够声明它们的实例,像这样使用lambda表达式?

Ordering order = (a, b) -> ...;

CacheLoader loader = (key) -> ...;

这样做引发的最常见的争论就是会增加阅读lambda的难度。以这种方式实例化一段抽象类将导致隐藏代码的执行:抽象类的构造方法。

另一个原因是,它抛出了lambda表达式可能的优化。在未来,它可能是这种情况,lambda表达式都不会计算到对象实例。放任用户用lambda来声明抽象类将妨碍像这样的优化。

外,有一个简单地解决方法。事实上,上述两个摘自Guava 库的实例类已经证明了这种方法。增加工厂方法将lambda转换成实例。

 

Ordering order = Ordering.from((a, b) -> ...);
CacheLoader loader = CacheLoader.from((key) -> ...);

要深入阅读,请参看由 Brian Goetz所做的说明: response to "Allow lambdas to implement abstract classes"。

What are the new features of Java8?
What are the new features of Java8?
翻译于 4年前
2人顶
 翻译得不错哦!
 

java.util.function

Package summary: java.util.function

As an early proof of Comparator and Runnable, it has been defined in the JDK The interface happens to be compatible with lambdas expressions as a functional interface. In the same way you can define any functional interface or third-party library in your own code.

But there is a specific form of function interface, which is extensive and universal and did not exist in the previous JD card. A large number of interfaces have been added to the new java.util.function package. Here are some of them:

  • Function -T as input, returns R as output

  • Predicate -T as input, the returned boolean value as output

  • ##Consumer -T as input, perform some action but no return value

  • Supplier - Without any input, return T

  • BinaryOperator -Two T as input, return one T as output, useful for "reduce" operation

These most primitive characteristics also exist. They are provided as int, long and double. For example:

  • IntConsumer - takes int as input, performs a certain action, and has no return value

There are some performance reasons here, mainly Interpretation avoids boxing and unboxing operations during input or output.

What are the new features of Java8? Waiting for PM
Translated 4 years ago
2 People like
like Very good translation!
  • 1

  • 2

  • ##3
  • >
##All translations in this article are for learning and communication purposes only. Please be sure to indicate the translator, source, and link to this article when reprinting
Our translation work complies with the CC agreement. If our work infringes upon your rights, please contact us in time
Comment (
85
)

The above is the detailed content of What are the new features of Java8?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:First essayNext article:First essay