简体   繁体   English

为什么 Eclipse 说 null 是一种原始类型?

[英]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.

相关问题 为什么在这种情况下为原始工作返回 null? - Why does returning null for a primitive work in this case? 为什么它说最低的数字总是空? - Why does it say the lowest number is Null always? 为什么Java编译器允许在三元运算符中将null转换为基本类型 - Why does Java compiler allow casting null to primitive type in ternary operator 为什么按钮说是无效的按钮类型? - Why does the button say as an invalid button type? 为什么 Eclipse 说“存在错误”但在控制台中什么也没显示? - Why does Eclipse say that “errors exist” but shows nothing in console? 为什么eclipse说即使使用局部变量也不使用? - Why does eclipse say Local variable is not used even though it is used? 为什么Eclipse的Google Cloud Tools插件会显示“未找到项目”? - Why does Google Cloud Tools Plugin for Eclipse say “No Projects Found”? 为什么Map中的键必须是对象而不是基元,为什么我不能将原始类型用作映射中的键 - Why does a key in a Map must be an object and not a primitive and Why cant i use primitive type as key in a map 为什么Java编译器不喜欢原始int作为HashMap中值的类型? - Why does the Java compiler not like primitive int as type for values in HashMap? Java:为什么“long”原始类型不接受简单数字? - Java: why the “long” primitive type does not accept a simple number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM