Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

拥抱 Java 8 语言特性 #1963

Merged
merged 30 commits into from
Aug 14, 2017
Merged
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
0d8d2d4
Merge pull request #11 from xitu/master
tanglie1993 Jul 8, 2017
b372255
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 8, 2017
55cba04
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 8, 2017
477c6f0
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 8, 2017
c54fc36
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 11, 2017
32b48b7
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 11, 2017
5a26bd9
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 12, 2017
b7c2cc4
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 12, 2017
17671bb
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 12, 2017
d12dc47
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 12, 2017
11da0a5
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 12, 2017
954cd5d
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 13, 2017
3b3267e
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 13, 2017
6f208be
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 13, 2017
5b5e411
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 13, 2017
13d270b
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 13, 2017
40f46ad
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 21, 2017
9c61527
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 21, 2017
7782f88
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 25, 2017
0038cd6
调整译者和校对者链接格式
linhe0x0 Jul 26, 2017
9d215f7
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 26, 2017
295f36e
Update we-analyzed-thousands-of-coding-interviews-heres-what-we-learn…
tanglie1993 Jul 26, 2017
7f00846
Merge pull request #12 from xitu/master
tanglie1993 Jul 26, 2017
04b4b62
Update embracing-java-8-language-features.md
tanglie1993 Jul 26, 2017
1060372
Update embracing-java-8-language-features.md
tanglie1993 Jul 26, 2017
b424f91
Update embracing-java-8-language-features.md
tanglie1993 Jul 26, 2017
9f2e650
Update embracing-java-8-language-features.md
tanglie1993 Jul 26, 2017
c47423c
Update embracing-java-8-language-features.md
tanglie1993 Aug 5, 2017
93ad848
Update embracing-java-8-language-features.md
tanglie1993 Aug 7, 2017
ed1b3f9
Update embracing-java-8-language-features.md
linhe0x0 Aug 14, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 59 additions & 59 deletions TODO/embracing-java-8-language-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
> * 原文作者:[Jeroen Mols](https://jeroenmols.com/)
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner)
> * 本文永久链接:[https://github.com/xitu/gold-miner/blob/master/TODO/embracing-java-8-language-features.md](https://github.com/xitu/gold-miner/blob/master/TODO/embracing-java-8-language-features.md)
> * 译者:
> * 译者:[tanglie1993](https://github.com/tanglie1993)
> * 校对者:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

校对者信息要加一下啦


# Embracing Java 8 language features
# 拥抱 Java 8 语言特性

For years Android developers have been limited to Java 6 features. While RetroLambda or the experimental Jack toolchain would help, proper support from Google was notably missing.
近年来,Android 开发者一直被限制在 Java 6 的特性中。虽然 RetroLambda 或者实验性的 Jack toolchain 会有一定帮助,来自 Google 官方的适当支持却一直缺失。

Finally, Android Studio 3.0 brings (backported!) support for most Java 8 features. Continue reading to learn how those work and why you should upgrade.
终于, Android Studio 3.0 带来了(已经向后移植!)对大多数 Java 8 特性的支持。继续阅读,你将看到其中的原理,以及升级的理由。

## Enabling java 8 features
## 引入 Java 8 特性

While Android Studio already supported many features in the [Jack toolchain](https://developer.android.com/guide/platform/j8-jack.html), starting from Android Studio 3.0 they are supported in the default toolchain.
虽然 Android Studio 已经支持 [Jack toolchain](https://developer.android.com/guide/platform/j8-jack.html) 中的大量特性,从 Android Studio 3.0 开始,它们会在默认的工具链中被支持。

First of all, make sure you disable Jack by removing the following from your main `build.gradle`:
首先,确保你已经把以下内容从你的主要 `build.gradle` 中移除,从而关闭了 Jack:

```
android {
Expand All @@ -31,7 +31,7 @@ android {
}
```

And add the following configuration instead:
然后加入以下的配置:

```
android {
Expand All @@ -43,7 +43,7 @@ android {
}
```

Also make sure you have the latest Gradle plugin in your root `build.gradle` file:
并且确保你在根 `build.gradle` 文件中有最新的 Gradle 插件:

```
buildscript {
Expand All @@ -54,13 +54,13 @@ buildscript {
}
```

Congratulations, you can now use most Java 8 features on all API levels!
恭喜,你现在可以在所有的 API 层级上使用大多数的 Java 8 特性了!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用大多数的 Java 8 特性了! -> 使用绝大多数的 Java 8 特性了!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么?我觉得两个都可以。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个确实差不多,都可以接受啦,就是看到most有点强迫症...


> Note: In case you’re migrating from [RetroLambda](https://github.com/evant/gradle-retrolambda), the official documentation has a more extensive [migration guide](https://developer.android.com/studio/write/java8-support.html#migrate).
> 注意:如果你在从 [RetroLambda](https://github.com/evant/gradle-retrolambda) 迁移过来,官方文档有一个更加全面的 [迁移指南](https://developer.android.com/studio/write/java8-support.html#migrate)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果你在从 -> 如果你要从

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

migrating是现在进行时,所以应该是“在”而不是“将要”。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

be doing有表示将来时,这边我觉得根据语境可能是将要比较合适一点?


## Lambda’s
## 有关 Lambda

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有关 Lambda -> 有关 Lambda 表达式
我觉得Lambda一般都指的是Lambda Expression,所以可能翻译成Lambda表达式更为确切一点?


Passing a listener to another class in Java 6 is quite verbose. A typical case would be where you add an `OnClickListener` to a `View`:
Java 6 中,向另一个类传入监听器的代码是相当冗长的。典型的情况是,你需要向 `View` 添加一个 `OnClickListener`:

```
button.setOnClickListener(new View.OnClickListener() {
Expand All @@ -71,83 +71,83 @@ button.setOnClickListener(new View.OnClickListener() {
});
```

Lambda’s can dramatically simplify this to the following:
Lambda 可以把它剧烈简化到下面这样:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lambda 可以把它剧烈简化到下面这样 -> Lambda 表达式可以把它显著地简化成下面这样


```
button.setOnClickListener(view -> doSomething());
```

Notice that almost all boilerplate is removed: no access modifier, no return type and no method name!
注意:几乎全部模板代码都被删除了:没有访问控制修饰符,没有返回值,也没有方法名称!

Now how do lambda’s actually work?
Lambda 究竟是怎么工作的呢?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lambda 究竟是怎么工作的呢? -> Lambda 表达式究竟是怎么工作的呢?


They are syntactic sugar that reduce the need for anonymous class creation whenever you have an interface with exactly one method. We call such interfaces functional interfaces and `OnClickListener` is an example:
它们使语法糖,当你有一个只有一个方法的接口时,它们可以减少创建匿名类的需要。我们把这些接口称为功能接口,`OnClickListener` 就是一个例子:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

它们使语法糖 -> 它们是语法糖


```
// A functional interface has exactly one method
// 只有一个方法的功能接口
public interface OnClickListener {
void onClick(View view);
}
```

Basically the lambda consists out of a three parts:
基本上 lambda 包括三个部分:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

基本上 lambda 包括三个部分: -> 基本上 lambda 表达式包括三个部分:


```
button.setOnClickListener((view) -> {doSomething()});
```

1. declaration of all method arguments between brackets `()`
2. an arrow `->`
3. code that needs to execute between brackets `{}`
1. 括号 `()` 中所有方法参数的声明
2. 一个箭头 `->`
3. 括号 `{}` 中需要执行的代码

Note that in many cases even the brackets `()` and `{}` can be removed. For more details have a look at the [official documentation](https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html).
注意:在很多情况下,甚至 `()` `{}` 这样的括号也可以被移除。更多细节,参见 [官方文档](https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html)

## Method references
## 方法引用

Recall that lambda expressions remove a lot of boilerplate code for functional interfaces. Method references take that concept one step further when the lambda calls a method that already has a name.
回忆一下 lambda 表达式为功能接口删除了大量样板代码的情形。当 lambda 调用了已经有一个名字的方法时,方法引用把这个概念更推进了一步。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

回忆一下 lambda 表达式为功能接口删除了大量样板代码的情形 -> 回忆一下 lambda 表达式为功能接口删除了大量样板代码的例子
这里可能用“例子”比较合适?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当 lambda 调用了已经有一个名字的方法时 -> 当 lambda 表达式调用了已经有一个名字的方法时

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

调用了一个已经有名字的方法时


In the following example:
在下面的例子中:

```
button.setOnClickListener(view -> doSomething(view));
```

All the lambda does is redirecting the work to an existing `doSomething()` method. In such a case, a method reference simplifies things further to:
Lambda 只是把要做的所有事情重定向到已有的 `doSomething()` 方法。在这种情况下,方法引用把事情简化到:

```
button.setOnClickListener(this::doSomething);
```

Note that the referenced method must take exactly the same parameters as the functional interface:
注意,被引用的方法必须和功能接口接收相同的参数:

```
// functional interface
// 功能接口
public interface OnClickListener {
void onClick(View view);
}

// referenced method: must take View as argument, because onClick() does
// 被引用的方法:必须接收 View 作为参数,因为 onClick() 会这样做:
private void doSomething(View view) {
// do something here
}
```

So how do method references work?
那么,方法引用是如何工作的呢?

They are again syntactic sugar to simplify a lambda expression that invokes an existing method. They can reference to:
它们是语法糖,可以简化调用了现有方法的 lambda 表达式。他们可以引用:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

它们是语法糖 -> 它们同样是语法糖


| static methods | MyClass::doSomething |
| instance method of object | myObject::doSomething |
| constructor | MyClass:: new |
| instance method of any argument type | String::compare |
| 静态方法 | MyClass::doSomething |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里应该是个表格,需要按照 Markdown 的语法调整一下哈。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我不理解。原文就不是表格啊。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

英文原文 是的,应该是在转化成 Markdown 的时候出现了失误。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可是markdown支持这个样子的表格吗?

| 对象的实例方法 | myObject::doSomething |
| 构造方法 | MyClass:: new |
| 任何参数类型的实例方法 | String::compare |

For more examples about this have a look at the [official documentation](https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html).
如果你需要更多关于这个的实例,请查看[官方文档](https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『官方文档』前面加个空格和其他地方保持一致哈。


## Default interface Methods
## 默认接口方法

Default methods make it possible to add new methods to an interface without breaking all classes that implement that interface.
默认方法使你可以在不破坏实现一个接口的所有的类的情况下,向接口中加入新的方法。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不破坏实现一个接口的所有的类的情况下 -> 不破坏实现了同一个接口的所有的类的情况下

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不……原文也是可以读通的。在这里“一个”和“同一个”的意思是一样的。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原文确实是通顺的,我觉得这边可能是要强调一下这个接口的“同一个”,并且下面半句里用了“该接口”,个人感觉这样可能更顺畅一点?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

向接口中加入新的方法 -> 向该接口中加入新的方法

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以在不破坏一个接口所有实现类的情况下


Imagine if you have a `MyView` interface that is implemented by a `MyFragment` (typical MVP scenario):
假设你有一个 `MyView` 接口,它被一个 `MyFragment` 实现(典型 MVP 场景):

```
public interface MyView {
Expand All @@ -163,9 +163,9 @@ public class MyFragment implements MyView {
}
```

When you now want to add an extra method to `MyView` your code will no longer compile, until `MyFragment` also implements that new method. This is annoying, and can be even problematic when many classes are implementing said interface.
如果你现在想要向 MyView 中加入一个额外的方法,你的代码将不再能够编译,直到 `MyFragment` 同样实现了这个新方法。这很烦人,并且如果很多类都实现这个接口的话,可能会引发新的问题。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你的代码将无法编译


Therefore Java 8 now allows you to define default methods that provide a standard implementation:
因此 Java 8 允许你定义带有标准实现的默认方法:

```
public interface MyView {
Expand All @@ -176,37 +176,37 @@ public interface MyView {
}
```

So how do default methods work?
那么默认方法是如何工作的呢?

Just define a method with the `default` keyword in the interface and provide an actual default method body.
在接口中定义一个带有 `default` 关键字的方法,并提供一个真实的默认方法体。

To learn more about this feature, have a look at the [official documentation](https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html).
要学习关于这个特性的更多知识,请查看 [官方文档](https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html)

## How to get started
## 如何开始

While this all might seem a bit overwhelming, Android Studio actually offers amazing quick fixes once you enable Java 8 features.
虽然这看起来有些吓人,但是一旦你打开了 Java 8 特性,Android Studio 就提供了非常好用的快速修复功能。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

虽然这么多东西可能让人有点无所适从,但是一旦你足够了解 Java 8 特性时,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enable并没有“了解”的意思吧?


Just use `alt/option` + `enter` to convert a functional interface to a lamba or a lambda to a method reference.
只要使用 `alt/option` + `enter` 就可以把功能接口转化为一个 lambda,或把 lambda 转为方法引用。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只要使用 alt/option + enter 就可以把功能接口转化为一个 lambda,或把 lambda 转为方法引用。 ->
只要使用 alt/option + enter 就可以把功能接口转化为一个 lambda 表达式,或把 lambda 表达式转为方法引用。


![Java 8 language quick fixes](https://jeroenmols.com/img/blog/java8language/androidstudioconversion.gif)
![Java 8 语言的快速修复功能](https://jeroenmols.com/img/blog/java8language/androidstudioconversion.gif)

This is a great way to get familiar with these new features and allows you to write code like you’re used to. After enough quick fixes by Android Studio you’ll learn in what cases a lambda or method reference would be possible and start writing them yourself.
这是一种熟悉新特性的好办法,它使你可以按照自己习惯的方式写代码。在使用 Android Studio 的快速修复功能足够多次之后,你将学会 lambda 和方法引用有哪些使用场景,并开始自己写它们。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你将学会 lambda 和方法引用有哪些使用场景,并开始自己写它们。 ->
你将学会 lambda 表达式和方法引用有哪些使用场景,并开始自己写它们。


## Supported features
## 支持的特性

While not all Java 8 features have been backported yet, Android Studio 3.0 offers plenty more features:
虽然并不是所有的 Java 8 特性都已经被向后移植,Android Studio 3.0 提供了很多其他的特性:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Android Studio 3.0 提供了很多其他的特性 -> 但是 Android Studio 3.0 仍然提供了很多其他的特性


- [static interface methods](https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html)
- [type annotations](https://docs.oracle.com/javase/tutorial/java/annotations/type_annotations.html)
- [repeating annotations](https://docs.oracle.com/javase/tutorial/java/annotations/repeating.html)
- [try with resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) (all versions, no longer min SDK 19)
- [静态接口方法](https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html)
- [类型标记](https://docs.oracle.com/javase/tutorial/java/annotations/type_annotations.html)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

类型标记 -> 类型注解

- [重复标记](https://docs.oracle.com/javase/tutorial/java/annotations/repeating.html)
- [针对资源的 try 语句](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) (all versions, no longer min SDK 19)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这句英文也应该翻译吧。

- Java 8 APIs (e.g. stream) -> min SDK 24

## Wrap-up
## 收尾

Thanks to Java 8 features, a lot of code can be simplified into lambda’s or method references. Android Studio auto convert is the easiest way to start learning these features.
Java 8 特性使得很多代码可以被简化为 lambda 表达式或方法引用。 Android Studio 自动转化是最简单的开始学习这些特性的方式。

If you’ve made it this far, you should probably follow me on [Twitter](https://twitter.com/molsjeroen). Feel free leave a comment below!
如果你已经读到这里了,你很可能应该在 [Twitter](https://twitter.com/molsjeroen) 上关注我。欢迎评论!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你应该在

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably



---
Expand Down