java运算符的含义" - >" [英] Meaning of java operator "->"
问题描述
java运算符 - >的含义是什么,如下面的代码所示,取自SpringBoot应用程序的初始化:
What is the meaning of the java operator "->" like in the following code, taken from the initialization of a SpringBoot application:
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return (container -> {
ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
ErrorPage error403Page = new ErrorPage(HttpStatus.FORBIDDEN, "/403.html");
ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
container.addErrorPages(error401Page, error403Page, error404Page, error500Page);
});
}
推荐答案
这是针对 lambda表达式,这是Java 8中首次引入的语言特性。基本上这个是一个内联匿名函数,它将 container
作为参数。通常lambdas返回值,但在这里它看起来只是执行调用 addErrorPages
到容器
的副作用 。没有为容器
指定类型,因为Java会从上下文中直接显示它。
This is for a lambda expression, a language feature first introduced in Java 8. Basically this is an inline anonymous function that takes container
as a parameter. Usually lambdas return values but here it looks like it is just carrying out the "side effect" of calling addErrorPages
to container
. There is no type specified for container
as Java intuits it from the context.
Lambda表达式不仅仅是一种语言功能,它们也是计算机科学和函数式编程的整个领域。描述它们的好SO帖子是这里。
Lambda expressions are more than a language feature, they are also a whole area of computer science and functional programming. A good SO post describing them is here.
这篇关于java运算符的含义" - >"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!