I've noticed eclipse JDT uses void
as a primitive type. Can this be considered correct?

- 8,227
- 12
- 49
- 68
-
I'm wondering whether `void` denotes a type at all? Or is it merely a syntactical placeholder? From the description of chapter 4 of the langspec, it certainly sounds like `void` isn't a type at all. – Johannes Schaub - litb Jan 13 '11 at 15:19
-
I've confirmed my suspicion. `void` is, pedantically speaking, not a type. – Johannes Schaub - litb Jan 13 '11 at 16:07
-
@"Johannes Schaub - litb" - can you give us the exact reference that enlightened you? – John Assymptoth Jan 13 '11 at 16:15
-
@Lord already did. It was the same thing I read and what finally convinced me. :) – Johannes Schaub - litb Jan 13 '11 at 16:40
7 Answers
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:
the Java programming language does not allow a "cast to void" — void is not a type http://java.sun.com/docs/books/jls/third_edition/html/statements.html#5989 (emphasis mine)
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.
-
1Personally, I'm baffled why modern programming languages haven't followed the nerdcore convention of naming "void" using a recursive acronym, e.g. VINAT: VINAT Is Not A Type. :) – Dan J Feb 22 '11 at 17:26
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 Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.
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:
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.

- 8,153
- 1
- 29
- 29
From Java 6 API docs:
public boolean isPrimitive() - Determines if the specified Class object represents a primitive type.
Returns: true if and only if this class represents a primitive type
I checked for myself:
void.class.getName() // void (OK)
void.class.isPrimitive() // true (??)
Void.class.getName() // java.lang.Void (OK)
Void.class.isPrimitive() // false (OK)
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:
public boolean isPrimitive() - Determines if the specified Class object represents a primitive type or a keyword void.
Returns: true if and only if this class represents a primitive type or a keyword void.

- 36,988
- 6
- 90
- 137
-
I wouldn't call it a bug, but yes, it can be very misleading. And the JLS doesn't explain that case very well. – John Assymptoth Feb 16 '11 at 12:26
-
It might be misleading, but it is definately not a bug, if you bother to further read the javadoc: "There are nine predefined Class objects to represent the **eight primitive types and void**. These are created by the Java Virtual Machine, and have the same names as the **primitive types** that they represent, namely **boolean, byte, char, short, int, long, float, and double**." – Dorian Gray Mar 14 '21 at 13:22
-
From your link:
Note that "void" is special in that its only legitimate uses are as a method return type and as a type literal.
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.

- 342,105
- 78
- 482
- 720
as I know, void its not a primitive type. However they have this constant in the class Type for reflection reasons!

- 11,840
- 8
- 60
- 73
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.

- 114,158
- 16
- 130
- 208
-
1A special primitive type is still a primitive type though. So the word "special" doesn't solve anything does it? – Johannes Schaub - litb Jan 13 '11 at 15:34
-
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.

- 345
- 1
- 3
- 12
-
If you read the documentation of the `isPrimitive()` function, it makes it pretty clear that `void` is not a primitive: "There are nine predefined Class objects to represent the **eight primitive types and void**. These are created by the Java Virtual Machine, and have the same names as the **primitive types** that they represent, namely **boolean, byte, char, short, int, long, float, and double**." – Dorian Gray Mar 14 '21 at 13:17