Why can't the var keyword in Java be assigned a lambda expression?

前端 未结 7 1300
一整个雨季
一整个雨季 2021-02-01 12:10

It is allowed to assign var in Java 10 with a string like:

var foo = \"boo\";

While it is not allowed to assign it with a lambda e

7条回答
  •  南旧
    南旧 (楼主)
    2021-02-01 12:42

    Because that is a non-feature:

    This treatment would be restricted to local variables with initializers, indexes in the enhanced for-loop, and locals declared in a traditional for-loop; it would not be available for method formals, constructor formals, method return types, fields, catch formals, or any other kind of variable declaration.

    http://openjdk.java.net/jeps/286

提交回复
热议问题