Patrick Beam
Patrick Beam

Reputation: 149

Is void a type?

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

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

Upvotes: 14

Views: 11093

Answers (8)

lukastymo
lukastymo

Reputation: 26809

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

Upvotes: 12

Mathias Schwarz
Mathias Schwarz

Reputation: 7197

void is a type in the Java language (you can read that directly in the Java Language Specification). However the type void has no member values, that is no concrete value will ever have the type void. void is therefore used to indicate that a method cannot return a value when called (that is void is the type of "no value").

Upvotes: -1

Go Dan
Go Dan

Reputation: 15502

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.

Upvotes: 10

CharithJ
CharithJ

Reputation: 47510

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

Upvotes: 2

Stealth Rabbi
Stealth Rabbi

Reputation: 10346

Any class method must specify a return type. The 'void' keyword can be specified, specifying that it has no return type, for example:

public void SetPantsSize(int width);

There is also a void class: http://download.oracle.com/javase/6/docs/api/java/lang/Void.html

Upvotes: 1

Valeriy Van
Valeriy Van

Reputation: 1865

Yes, void is definitely type, meaning 'nothing'. By the way, do you mean C (C++, Objective-C, ...). Couldn't say for any language, but in C (C++, Objective-C, ...) void is a type.
But it is special data type. You couldn't declare variable of type void. That's differ void from any other types. But you could declare pointer to void. Function which return value is void means function has no return value, or returns nothing. That's afraid all cases where void type could be used.
void * v; /** declares variable v as pointer to void */
*v used as left value in expression could be assigned value of any type without type cast. That's why void type was introduced into language.

You updated your question, underlining you were asking about Java. Java has no pointers and no functions. void type is used in declarations of methods returning nothing.

Upvotes: 1

Gareth
Gareth

Reputation: 69

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?

Upvotes: 2

driis
driis

Reputation: 164291

Void is a type in most languages, and given the typical type system, it makes the most sense to think about it as a type that specifies "nothing". You didn't state your language, but for example, in C#, the void keyword corresponds to the .NET framework void type.

Upvotes: 0

Related Questions