Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 285
John Assymptoth Avatar asked Jan 13 '11 15:01

John Assymptoth


People also ask

What are considered primitive in Java?

Primitive types are the most basic data types available within the Java language. There are 8: boolean , byte , char , short , int , long , float and double . These types serve as the building blocks of data manipulation in Java. Such types serve only one purpose — containing pure, simple values of a kind.

What type is void in Java?

The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.

Which is not a Java primitive type?

Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc. You will learn more about these in a later chapter.

Is void a return type in Java?

Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so.


1 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.

like image 107
Pops Avatar answered Sep 21 '22 17:09

Pops