Java 9下划线
在Java的早期版本中,下划线可以用作标识符并还可以创建变量名。但是在Java 9发行版中,下划线是一个关键字,不能用作标识符或变量名。
如果我们使用下划线字符("_")作为标识符,则源代码不能
让我们看看一些示例,这些示例说明了下划线之后的使用情况如何更改。
在Java 7中,我们可以像下面这样使用下划线。
Java 7下划线示例
public class UnderScoreExample {
public static void main(String[] args) {
int _ = 10; // creating variable
System.out.println(_);
}
}
它产生的输出没有任何警告和错误。
输出:
Java 8下划线示例
如果我们使用Java 8编译相同的程序,它将编译但会发出警告消息。
public class UnderScoreExample {
public static void main(String[] args) {
int _ = 10;
System.out.println(_);
}
}
输出:
UnderScoreExample.java:3: warning: '_' used as an identifier
int _ = 10;
^
(use of '_' as an identifier might not be supported in releases after Java SE 8)
Java 9下划线示例
在Java 9中,程序现在无法编译并引发编译时错误,因为它现在是关键字并且不能用作变量名称。
public class UnderScoreExample {
public static void main(String[] args) {
int _ = 10;
System.out.println(_);
}
}
输出:
UnderScoreExample.java:3: error: as of release 9, '_' is a keyword, and may not be used as an identifier
int _ = 10;