javalambdajava-8

_ (underscore) is a reserved keyword


I've just replaced s in the following lambda expression by _:

s -> Integer.parseInt(s)

Eclipse compiler says:

'_' should not be used as an identifier, since it is a reserved keyword from source level 1.8 on.

I haven't found any explanation in the JLS §3.9 Lexical Structure / Keywords.


Solution

  • The place to look is JLS §15.27.1. Lambda Parameters

    It is a compile-time error if a lambda parameter has the name _ (that is, a single underscore character).

    The use of the variable name _ in any context is discouraged. Future versions of the Java programming language may reserve this name as a keyword and/or give it special semantics.

    So the Eclipse message is misleading, especially as the same message is used for both cases, when an error is generated for a lambda parameter or when a warning is generated for any other _ identifier.