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.
In earlier versions of Java, the underscore ("_") has used as an identifier or to create a variable name. Since Java 9, the underscore character is a reserved keyword and can't be used as an identifier or variable name.
In Java SE 7 and later, any number of underscore characters ( _ ) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With