Java->操作员
我知道- >
用于使用语法() - > {}
的lambda表达式。
但是我看到了此代码:file-> file.isfile()
- 没有()
和{}
。它做什么?
I know that ->
is used for lambda expressions with the syntax ()->{}
.
But I saw this code: file -> file.isFile()
- with no ()
and {}
. What does it do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
()
当您有一个参数时,不需要{}
当您的lambda主体是单个表达式时,不需要。参见 java tutorial a href =“ https://docs.oracle.com/javase/specs/jls/se17/sse17/html/jls-15.html#jls-15.27” rel =“ nofollow noreferrer”> java specification 语法的形式描述。
()
is not required when you have a single argument and{}
is not required when your lambda body is a single expression.See the Java tutorial for further information and the Java Specification for a more formal description of the syntax.
正如费德里科(Federico)回答的那样,对于单一参数,您不需要
()
,对于,语句仅返回一个值,并且不做其他任何事情,您不需要{}
。我想为他的贡献做出的添加:
您提出的代码表示,有一个函数,将文件作为参数。然后,返回其
.filile()
。换句话说,您也可以通过定义一种方法来达到相同的目标:
lambda功能使其短一点。如果您不打算重复使用该方法,则最好使用lambda。
As Federico answered, for the single arguments you do not need
()
and for the statements only return a value and do not do anything else, you do not need{}
.The addition I want to make to his contribution:
The code you put means, there is a function, that takes file as a parameter. Then, returns its
.isFile()
.In other words, you can also reach the same aim by defining a method:
Lambda function just makes it a little bit shorter. If you are not going to re-use the method, may be better to use lambda.