Java9中下划线关键字的用法

在Java的早期版本中,下划线 _)用作标识符或创建变量。从Java 9开始,下划线字符是保留关键字,不能用作标识符或变量名。如果我们使用单个下划线字符作为标识符,则该程序将无法编译并抛出编译时错误,因为它现在是关键字,在Java 9或更高版本中不能用作变量名。

示例

public class UnderscoreKeywordTest {
   public static void main(String args[]) {
      int _ = 50
      System.out.println(_);
   }
}

输出结果

UnderscoreKeywordTest.java:3: error: as of release 9, '_' is a keyword, and may not be used as an identifier
int _ = 50;
^
UnderscoreKeywordTest.java:4: error: as of release 9, '_' is a keyword, and may not be used as an identifier
System.out.println(_);