Java _(下划线)是保留关键字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23523946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
_ (underscore) is a reserved keyword
提问by Aubin
I've just replaced s
in the following lambda expression by _
:
我刚刚s
在以下 lambda 表达式中替换为_
:
s -> Integer.parseInt(s)
Eclipse compiler says:
Eclipse 编译器说:
'_' should not be used as an identifier, since it is a reserved keyword from source level 1.8 on.
'_' 不应用作标识符,因为它是从源代码级别 1.8 开始的保留关键字。
I haven't found any explanation in the JLS §3.9Lexical Structure / Keywords.
我在JLS §3.9词法结构/关键字中没有找到任何解释。
采纳答案by Holger
The place to look is JLS §15.27.1. Lambda Parameters
要看的地方是JLS §15.27.1。Lambda 参数
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.
如果 lambda 参数具有名称 _(即单个下划线字符),则会出现编译时错误。
不鼓励在任何上下文中使用变量名称 _。Java 编程语言的未来版本可能会将此名称保留为关键字和/或赋予它特殊的语义。
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.
因此 Eclipse 消息具有误导性,尤其是在两种情况下使用相同的消息时,当为 lambda 参数生成错误或为任何其他_
标识符生成警告时。
回答by Angel Koh
Java Language Changes for Java SE 9https://docs.oracle.com/javase/9/language/toc.htm#JSLAN-GUID-16A5183A-DC0D-4A96-B9D8-AAC9671222DD
Java SE 9 的 Java 语言更改https://docs.oracle.com/javase/9/language/toc.htm#JSLAN-GUID-16A5183A-DC0D-4A96-B9D8-AAC9671222DD
From Java 9, the _ character cannot be used as an identifier anymore, not just within the lambda context
从 Java 9 开始,_ 字符不能再用作标识符,而不仅仅是在 lambda 上下文中
The underscore character is not a legal name.
If you use the underscore character ("_") an identifier, your source code can no longer be compiled.
下划线字符不是合法名称。
如果您使用下划线字符(“_”)作为标识符,您的源代码将无法再编译。
回答by Alexandre de Champeaux
It is the Phase 2 of JEP 302, that is going to add underscore as a special character to denote unused parameters in lambda expressions.
这是JEP 302的第 2 阶段,将添加下划线作为特殊字符来表示 lambda 表达式中未使用的参数。
Treatment of underscores
In many languages, it is common to use an underscore (
_
) to denote an unnamed lambda parameter (and similarly for method and exception parameters):
BiFunction<Integer, String, String> biss = (i, _) -> String.valueOf(i);
This allows stronger static checking of unused arguments, and also allows multiple arguments to be marked as unused. However, because underscore was a valid identifier as of Java 8, compatibility required us to take a more indirect path to getting to where underscore could serve this role in Java. Phase 1 was forbidding underscore as a lambda formal parameter name in Java 8 (this had no compatibility consequence, since lambdas did not exist previously) and a warning was issued for using underscore as an identifier in other places. Phase 2 came in Java 9, when this warning became an error. We are now free to complete the planned rehabilitation of underscore to indicate an unused lambda, method, or catch formal parameter.
下划线的处理
在许多语言中,通常使用下划线 (
_
) 来表示未命名的 lambda 参数(对于方法和异常参数也是如此):
BiFunction<Integer, String, String> biss = (i, _) -> String.valueOf(i);
这允许对未使用的参数进行更强的静态检查,并且还允许将多个参数标记为未使用。然而,因为下划线是 Java 8 的有效标识符,兼容性要求我们采取更间接的路径来到达下划线在 Java 中可以发挥这一作用的地方。阶段 1 禁止在 Java 8 中将下划线作为 lambda 形式参数名称(这没有兼容性后果,因为之前不存在 lambdas)并且发出警告以在其他地方使用下划线作为标识符。第 2 阶段出现在 Java 9 中,当此警告变成错误时。我们现在可以自由完成下划线的计划修复,以指示未使用的 lambda、方法或捕获形式参数。