In Java, can "void" be considered a primitive type?

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.


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.


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.


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.