[英]Why does Eclipse say that null is a primitive type?
I wrote this line of code in Eclipse Mars for messing purposes:我在 Eclipse Mars 中写了这行代码,目的是为了搞砸:
null.toString();
And I got the following compiler error message:我收到以下编译器错误消息:
Cannot invoke toString() on the primitive type null
无法对原始类型 null 调用 toString()
Which is very strange since null
is not a primitive type nor an object reference as explained here: Is null an Object?这很奇怪,因为
null
既不是原始类型,也不是对象引用,如下所述: Is null an Object?
So, just to be sure, I tried to compile such odd line of code using javac
and I got this result:所以,可以肯定的是,我尝试使用
javac
编译这样奇怪的代码行,我得到了这个结果:
NullTest.java:3: <nulltype> cannot be dereferenced null.toString(); ^ 1 error
Does somebody know why Eclipse would give such (IMO) misleading compiler error message?有人知道为什么 Eclipse 会给出这样的(IMO)误导性编译器错误消息?
Since the null-type is a subtype of Object
, it's conceivably OK to invoke Object
methods on null
.由于 null 类型是
Object
的子类型,可以想象在null
上调用Object
方法是可以的。
However, following that logic, since the null-type is a subtype of every reference type, we should be allowed to invoke any method of any class/interface on null
.然而,按照这个逻辑,由于 null 类型是每个引用类型的子类型,我们应该被允许在
null
上调用任何类/接口的任何方法。 That'll be a mess.那将是一团糟。
Syntactically, null.toString()
should be recognized as a method invocation expression at first, because null
is a Primary
expression.在语法上,
null.toString()
应该被识别为方法调用表达式,因为null
是一个Primary
表达式。 Then, to determine the class/interface to search for the method toString
, JLS says然后,为了确定搜索
toString
方法的类/接口,JLS 说
...The class or interface to search is T if T is a class or interface type, or the upper bound of T if T is a type variable
...如果 T 是类或接口类型,则要搜索的类或接口为 T,如果 T 是类型变量,则为 T 的上限
It is a compile-time error if T is not a reference type.
如果 T 不是引用类型,则会出现编译时错误。
T
is the null-type here; T
是这里的空类型; it is not a class type, interface type, or type variable, therefore this step should fail.它不是类类型、接口类型或类型变量,因此这一步应该失败。 However, does it fail because T is not a reference type ?
但是,它是否会因为T 不是引用类型而失败?
Is the null-type a reference type? null 类型是引用类型吗? JLS says
JLS说
The types ... are divided into two categories: primitive types and reference types
类型...分为两类:原始类型和引用类型
The numeric types are ....
数字类型是....
The reference types are class types, interface types, [type variables,] and array types .
引用类型是类类型、接口类型、[类型变量]和数组类型。 [period!]
[时期!]
There is also a special null type .
还有一种特殊的空类型。
Depending on your parsing of the text, null-type may or may not be a reference type.根据您对文本的解析,null-type 可能是也可能不是引用类型。 That is usually not really important;
这通常并不重要。 it's just a matter of categorization.
这只是一个分类问题。 But it leads to confusions, for example in this case -- the failure is because
T
is not a "proper" reference type, and the compiler deduces by mistake that it must be a primitive type then.但它会导致混淆,例如在这种情况下 - 失败是因为
T
不是“正确的”引用类型,并且编译器错误地推断它必须是原始类型。
There is no reason.没有理由。
Usually eclipse analyze the bytecode and for some reason null.toString()
are two bytecode-statements, a definition and a call.通常 eclipse 会分析字节码,并且出于某种原因
null.toString()
是两个字节码语句,一个定义和一个调用。 Because you can call toString
only on objects, eclipse assumes the declaraion must declare a primitive.因为您只能在对象上调用
toString
,所以 eclipse 假定声明必须声明一个原语。
You can write a Bugreport but think about the overhead that may come with the internationalization of the error-message.您可以编写错误报告,但请考虑错误消息国际化可能带来的开销。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.