Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
andyalgorithm
toBeTopJavaer
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
toBeTopJavaer
/
docs
/
basics
/
java-basic
/
lambda.md
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
58 lines (37 loc) · 2.09 KB
master
Breadcrumbs
toBeTopJavaer
/
docs
/
basics
/
java-basic
/
lambda.md
Copy path
Top
File metadata and controls
Code
Blame
58 lines (37 loc) · 2.09 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
Lambda
表达式
,
也可称为闭包
,
它是推动
Java
8
发布的最重要新特性
。
Lambda
允许把函数作为一个方法的参数
(
函数作为参数传递进方法中
)。
使用
Lambda
表达式可以使代码变的更加简洁紧凑
。
###
语法
lambda
表达式的语法格式如下
:
```
(
parameters
) ->
expression
或
(
parameters
) ->{
statements
; }
```
以下是lambda表达式的重要特征
:
*
可选类型声明
:
不需要声明参数类型
,
编译器可以统一识别参数值
。
*
可选的参数圆括号
:
一个参数无需定义圆括号
,
但多个参数需要定义圆括号
。
*
可选的大括号
:
如果主体包含了一个语句
,
就不需要使用大括号
。
*
可选的返回关键字
:
如果主体只有一个表达式返回值则编译器会自动返回值
,
大括号需要指定明表达式返回了一个数值
。
###
Lambda
表达式实例
```
// 1. 不需要参数,返回值为 5
() ->
5
// 2. 接收一个参数(数字类型),返回其2倍的值
x
->
2
*
x
// 3. 接受2个参数(数字),并返回他们的差值
(
x
,
y
) ->
x
–
y
// 4. 接收2个int型整数,返回他们的和
(int
x
, int
y
) ->
x
+
y
// 5. 接受一个 string 对象,并在控制台打印,不返回任何值(看起来像是返回void)
(
String
s
) ->
System
.
out
.
print
(
s
)
```
Lambda
表达式主要用来定义行内执行的方法类型接口
,
例如
,
一个简单方法接口
。
在上面例子中
,
我们使用各种类型的Lambda表达式来定义MathOperation接口的方法
。
然后我们定义了sayMessage的执行
。
Lambda
表达式免去了使用匿名方法的麻烦
,
并且给予Java简单但是强大的函数化的编程能力
。
###
变量作用域
lambda
表达式只能引用标记了
final
的外层局部变量
,
这就是说不能在
lambda
内部修改定义在域外的局部变量
,
否则会编译错误
。
如以下代码
,
编译会出错
:
```
String
first
=
""
;
Comparator
<
String
>
comparator
= (
first
,
second
) ->
Integer
.
compare
(
first
.
length
(),
second
.
length
());
```
原文地址
:
https
:
//www.runoob.com/java/java8-lambda-expressions.html
You can’t perform that action at this time.