在Java中,can“void”被认为是原始类型吗?
我注意到 eclipse JDT 使用 void
作为原始类型。这可以认为是正确的吗?
I've noticed eclipse JDT uses void
as a primitive type. Can this be considered correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
没有 void 不是原始类型。它只是一个关键字,表示方法没有返回值。最接近的是 java.lang.Void 类,Javadocs 中将其描述为:
JDT 中的存在仅仅是为了为代码构建 AST。如果您查看同一文档中的字段值描述,它会显示:
No void is not a primitive type. It is simply a keyword to indicate a method has no return value. The closest you can come is the java.lang.Void class, which from the Javadocs is described as:
The presence in the JDT is merely to build the ASTs for the code. If you look at the field value description in the same docs it says:
从 Java 6 API 文档< /a>:
我自己检查过:
这是错误吗?我知道 void 不是原始类型(我认为它只是关键字),但为什么 void.class.isPrimitive() 返回 true ?
编辑:
我认为应该澄清一下,所以我建议 java:doc bug 7019906。在我看来应该是:
From Java 6 API docs:
I checked for myself:
Is it bug ? I know that void is not primitive type (I think it is just keyword), but why void.class.isPrimitive() returns true ?
edit:
I think it should be clarified, so I suggested java:doc bug 7019906. In my opinion it should be:
从您的链接:
另请注意,这是一个与 AST 节点(即 Java 语言的语法)相关的类。
基本上,在对语言语法进行建模时,
void
出现在一些与原始类型相同的位置,因此当将语法表示为 Java 类时,您必须对其进行类似的分类。From your link:
Note also that this is a class concerned with AST nodes, i.e. the syntax of the Java language.
Basically, when modelling the language syntax,
void
appears in some of the same places as primitive types, so when representing the syntax as a Java class, you have to classify it similarly.据我所知, void 它不是原始类型。然而,出于反射原因,他们在 Type 类中拥有这个常量!
as I know, void its not a primitive type. However they have this constant in the class Type for reflection reasons!
这是您引用的 javadoc 中编写的内容:
原始类型“void”的类型代码。请注意,“void”是特殊,因为它唯一合法的用途是作为方法返回类型和类型文字。
注意粗体字。我认为这可以解释一切。
here is what written in javadoc you referenced:
Type code for the primitive type "void". Note that "void" is special in that its only legitimate uses are as a method return type and as a type literal.
Pay attention on the bold word. I think this explains everything.
我看到你们对此争论很多,但是...
嘿伙计们,java.lang.Class 中有一个名为 isPrimitive() 的函数,
那么我们为什么不使用 void 类对象调用它并获得答案呢?
此外,Void.TYPE是使用Class.getPrimitiveClass(“void”)获取的。
所以事实很清楚,void 是原始的。
I see you argue a lot about this but...
hey guys, there is a function named isPrimitive() in java.lang.Class
so why don't we invoke it with void class object and get the answer?
besides, Void.TYPE is get using Class.getPrimitiveClass("void").
So the fact is clear, void IS primitive.
我发现,在这种情况下,您最好查阅 Java 语言规范。很明显,
void
不是一个原语。首先,
void
不在 原始类型列表。后来,JLS明确指出:此外,
void
出现在 关键字列表,而不是文字列表。您看到自己所做的事情的原因是Michael Borgwardt 解释得很好。
所以,回答你的标题:不。在 Java 中,
void
不能被视为原语。回答你的问题:是的,Eclipse JDT 代码对于它需要做的事情来说是正确的。I find that, in cases like this, you can't beat going to the Java Language Specification. It is pretty clear about the fact that
void
is not a primitive.First off,
void
is not in the list of primitive types. Later on, the JLS explicitly states:Furthermore,
void
appears in the list of keywords, not the list of literals.The reason that you saw what you did was explained nicely by Michael Borgwardt.
So, to answer your title: no. In Java,
void
cannot be considered a primitive. To answer your body: yes, the Eclipse JDT code is correct for what it needs to do.