Java标识符,关键字,保留字三者区分(主要是从官网摘抄)

区分

标识符(Identifiers)

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.

关键字(keyword)

50 character sequences, formed from ASCII letters, are reserved for use as keywords and cannot be used as identifiers (现在已经变成了51个,除了const,goto,还包括了_)
The keywords const and goto are reserved, even though they are not currently used.
The keyword _ (underscore) is reserved for possible future use in parameter declarations.

易混

  • true and false are not keywords, but rather boolean literals
  • null is not a keyword, but rather the null literal
  • var and yield are not keywords, but rather restricted identifiers .
  • A further ten character sequences are restricted keywords: open, module, requires, transitive, exports, opens, to, uses, provides, and with.

保留字(reserved word)

没在官方文档上找到明确的解释
但提到不能被用做标识符的有这几类
An identifier cannot have the same spelling (Unicode character sequence) as a keyword, boolean literal , or the null literal , or a compile-time error occurs.

  1. 关键字(keyword)
  2. true, false
  3. null
    还有一个办法可以看是不是保留字,直接在编译器中试试
int var =9;//这个是可以的
var var = 9;//也是可以的
int const = 9;//不行,这也说明了,保留关键字其实也是被算作关键字的

总结

保留字如果是指不能被用户拿来做为标识符,那么关键字就是保留字的子集
保留字如果是指现阶段确定但未被使用的关键字,那么保留字就是关键字的子集

下面是官方文档链接,诸位可以自己去找
java8
java15

posted @ 2021-02-03 10:22  茕祇  阅读(202)  评论(0编辑  收藏  举报