Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is void a type?

Tags:

java

void

I can't answer completely "why should we call "void" is 'return type'?"

How do I prove that "void" is a type?

like image 414
Patrick Beam Avatar asked Sep 26 '11 19:09

Patrick Beam


4 Answers

void is not a type, it is not also a return type:

in JLS 14.8 you can find a note about this type:

Note that the Java programming language does not allow a "cast to void"-void is not a type

like image 183
lukastymo Avatar answered Oct 22 '22 03:10

lukastymo


The Java Language Specification says that:

[...] every variable and every expression has a type that can be determined at compile time. The type may be a primitive type or a reference type.

void is not a valid type. However, void is a valid keyword used to indicate that a method does not return a value.

The Void JavaDoc also says that void is a keyword:

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

Contrast that to the Integer JavaDoc:

The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.

The Void class represents the void keyword; while the Integer class represents an int type.

like image 35
Dan Cruz Avatar answered Oct 22 '22 04:10

Dan Cruz


Quote:

TYPE

public static final Class TYPE
The Class object representing the primitive Java type void.

Taken from : http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Void.html

Or have I misunderstood the question?

like image 2
Gareth Avatar answered Oct 22 '22 04:10

Gareth


Hope class void explains why void is a type.

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

public final class Void extends Object
like image 2
CharithJ Avatar answered Oct 22 '22 05:10

CharithJ