什么类型的令牌恰好是“var”。在Java 10? [英] What type of token exactly is "var" in Java 10?

查看:92
本文介绍了什么类型的令牌恰好是“var”。在Java 10?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上一期Heinz Kabutz的时事通讯中,#255 Java 10:推断的局部变量,显示 var 不是Java 10中的保留字,因为您还可以使用 var 作为标识符:

In the last issue of Heinz Kabutz's newsletter, #255 Java 10: Inferred Local Variables, it is shown that var is not a reserved word in Java 10, because you can also use var as an identifier:

public class Java10 {
    var var = 42; // <-- this works
}

但是,你不能使用ie 断言作为标识符,如 var assert = 2 ,因为断言是保留字。

However, you cannot use i.e. assert as an identifier, as in var assert = 2, because assert is a reserved word.

正如链接通讯中所述, var 不是保留的事实单词是个好消息,因为这允许使用 var 作为标识符的旧版Java代码在Java 10中编译时没有问题。

As it's told in the linked newsletter, the fact that var is not a reserved word is good news, because this allows code from previous versions of Java that uses var as an identifier to compile without problems in Java 10.

那么,什么是 var 呢?它既不是显式类型也不是语言的保留字,因此它被允许作为标识符,但是当它用于在Java 10中声明局部变量时它确实具有特殊含义。我们在一个上下文中究竟是什么称呼它局部变量声明?

So, what's var then? It's neither an explicit type nor a reserved word of the language, so it's allowed to be an identifier, however it does have a special meaning when used to declare a local variable in Java 10. What exactly do we call it in the context of a local variable declaration?

此外,除了支持向后兼容性(通过允许包含 var 的旧代码作为标识符)编译), var 是保留字还有其他优点吗?

Additionally, apart from supporting backwards compatibility (by allowing older code that contains var as an identifier to compile), are there other advantages to var not being a reserved word?

推荐答案

根据 JEP-286:局部变量类型推断 var


不是关键字;相反,它是一个保留类型名称

(早期版本的JEP留下了实施的空间保留类型名称或上下文相关关键字;最终选择了前一个路径。)

(Earlier versions of the JEP left room for implementing either as a reserved type name or as a context-sensitive keyword; the former path was ultimately chosen.)

因为它不是保留关键字,所以仍然可以使用它在变量名(和包名)中,但不在类或接口名中。

Because it's not a "reserved keyword", it is possible to still use it in variable names (and package names), but not in class or interface names.

我认为没有使 var <的最大原因/ code>保留关键字与旧源代码向后兼容。

I would think the biggest reason for not making var a reserved keyword is backwards compatibility with old source code.

这篇关于什么类型的令牌恰好是“var”。在Java 10?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆