When are braces optional in Java 8 lambda syntax?

前端 未结 4 2063
一向
一向 2020-11-29 10:23

I realise that the Java 8 lambda implementation is subject to change, but in lambda build b39, I\'ve found that braces can only be omitted when the lambda expression returns

4条回答
  •  遥遥无期
    2020-11-29 10:59

    I have tried your code and for the most up-to-date JRE version I think it would be ok.

    The following is what I reference from Oracle Java docs.

    In a lambda expression, you must enclose statements in braces ({}). However, you do not have to enclose a void method invocation in braces.
    For example, the following is a valid lambda expression:

    email -> System.out.println(email)
    

    And the doc explains quite clearly. Hope this can solve your problem.

    References: https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

提交回复
热议问题