在Java 9中,下划线关键字的用途是什么?

PHPz
PHPz 转载
2023-09-09 14:49:02 996浏览

在Java 9中,下划线关键字的用途是什么?

在早期版本的 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(_);

以上就是在Java 9中,下划线关键字的用途是什么?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除