Java,为什么不能用null作为包名呢?
Java, why can't we use null as package name?
我正在将 apitest 移植到 jogl,其中一个目录称为 Null,所以我只是想使用 null
作为包名,Netbeans 抱怨它无效。
我知道我在吹毛求疵,但我只是好奇..我在这里或 google..
上没有找到任何东西
是否与反射、名称检索、类似的东西或其他东西有关?
还是设计成这样,和运算符重载一样?
编辑:我知道这是一个保留字,但在代码中..
标识符不能命名为 null
,如 language specification 所述:
An identifier cannot have the same spelling (Unicode character sequence) as a keyword (§3.9), boolean literal (§3.10.3), or the null literal (§3.10.7), or a compile-time error occurs.
在这种情况下,null
是 java 语言中的文字 (null literal),不能用作标识符...
根据Oracle:
命名规则:
Package names are written in all lower case to avoid conflict with the
names of classes or interfaces.
...
在您的情况下,以下参考很重要:
In some cases, the internet domain name may not be a valid package
name. This can occur if the domain name contains a hyphen or other
special character, if the package name begins with a digit or other
character that is illegal to use as the beginning of a Java name, or
if the package name contains a reserved Java keyword, such as "int".
In this event, the suggested convention is to add an underscore. For
example:
我相信这是设计使然。
根据 Java 语言规范,'null' 是一个关键字,您不能使用任何关键字来标识其主要用途以外的任何其他内容。
虽然您可以将 Null 与大写 'N' 一起使用,但对于与预定义关键字大小写不匹配的所有其他关键字也类似。
Java 不鼓励使用这种糟糕的命名做法。
null 是赋予标识符的值。包是一个标识符。它不能是一个值。
我正在将 apitest 移植到 jogl,其中一个目录称为 Null,所以我只是想使用 null
作为包名,Netbeans 抱怨它无效。
我知道我在吹毛求疵,但我只是好奇..我在这里或 google..
上没有找到任何东西是否与反射、名称检索、类似的东西或其他东西有关? 还是设计成这样,和运算符重载一样?
编辑:我知道这是一个保留字,但在代码中..
标识符不能命名为 null
,如 language specification 所述:
An identifier cannot have the same spelling (Unicode character sequence) as a keyword (§3.9), boolean literal (§3.10.3), or the null literal (§3.10.7), or a compile-time error occurs.
在这种情况下,null
是 java 语言中的文字 (null literal),不能用作标识符...
根据Oracle:
命名规则:
Package names are written in all lower case to avoid conflict with the names of classes or interfaces.
...
在您的情况下,以下参考很重要:
In some cases, the internet domain name may not be a valid package name. This can occur if the domain name contains a hyphen or other special character, if the package name begins with a digit or other character that is illegal to use as the beginning of a Java name, or if the package name contains a reserved Java keyword, such as "int". In this event, the suggested convention is to add an underscore. For example:
我相信这是设计使然。
根据 Java 语言规范,'null' 是一个关键字,您不能使用任何关键字来标识其主要用途以外的任何其他内容。
虽然您可以将 Null 与大写 'N' 一起使用,但对于与预定义关键字大小写不匹配的所有其他关键字也类似。
Java 不鼓励使用这种糟糕的命名做法。
null 是赋予标识符的值。包是一个标识符。它不能是一个值。