java8-Lambda表达式02-Lamba表达式的语法

1、Lambda表达的语法, oracle官网给出的定义是:

A lambda expression is composed of three parts. 

Argument List Arrow Token Body
(int x, int y) -> x + y

The body can be either a single expression or a statement block. 

In the expression form, the body is simply evaluated and returned. 

In the block form, the body is evaluated like a method body and a return statement returns control to the caller of the anonymous method. 

The break and continue keywords are illegal at the top level, but are permitted within loops. 

If the body produces a result, every control path must return something or throw an exception.

翻译与注解:

lambda表达式由三部分构成:

  • 参数列表:小括号对是必需的,参数类型和参数可选的,参数个数是可变的。即可出现这样的参数列表形式: () 、(int a, int b)、(a, b, c)
  • 箭头标识符: 由减号和大于号构成, 且两个符号之间不能有空格
  • 体:既可以是单个表达式,也可以是语句块。如果表达式,进行简单计算求值后返回; 如果是语句块,像运行方法体和返回语句一样,将最终结果和控制数返回给匿名方法的调用者;break和continue关键性禁止出现在顶层上,但允许在循环中使用;如果执行体中有产出结果,每个控制路径必须有返回值,除非抛出异常。

2、Lambda表达式的几种形式

() -> express
(params) -> express
() -> {return express}
(params) -> {return express}
() ->statement
(params) -> statement
() -> {statements}
(params) -> {statements}

3、扩展问题(?)

return、break、continue在lambda表达式中的使用场景、特殊性

【技术的道路上,要知其然,更要知其所以然】
原文地址:https://www.cnblogs.com/weisiren/p/7711438.html