Josh
Josh

Reputation: 3284

java.lang.Void vs void vs Null

What exactly is the difference between Void, void, and can I just use null instead?

I'm asking this is because I'm looking at sample Android code where they used Void but Eclipse errors on it (it says Void cannot be resolved to a variable).

My code that breaks is

public class MyAsyncTask extends AsyncTask<Void, Void, Boolean>{
    ...
}

I use it like this

MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute((Void),null);//this is the line that breaks  "Void cannot be resolved to a variable"

Upvotes: 21

Views: 17980

Answers (5)

Subodh Karwa
Subodh Karwa

Reputation: 2725

void : java keyword indicates method will not return anything. Used for methods

Void : Wrapper around void. It extends Object class.

Needed as Generic does not support primitive datatypes. (void considered primitive is a complex case but this is just to explain)

Should be used for reflection

Constructor<Void> constructor = Void.class.getConstructor()

null indicates no values. Used for objects as comparison to void which is used for methods.

Upvotes: 1

jedyobidan
jedyobidan

Reputation: 926

You have an extra comma in your code.

myAsyncTask.execute((Void),null);
                        //^extra comma right here

Also, there is no need to cast null to Void, because (1) Void has no instances and thus there is no Void object, and (2) casting null to anything is rather useless because null is a valid value for any Object data type.

Code should probably just be:

myAsyncTask.execute(null);

Upvotes: 4

Javier
Javier

Reputation: 12398

The most common use of Void is for reflection, but that is not the only place where it may be used.

void is a keyword that means that a function does not result a value.

java.lang.Void is a reference type, then the following is valid:

 Void nil = null;

(so far it is not interesting...)

As a result type (a function with a return value of type Void) it means that the function *always * return null (it cannot return anything other than null, because Void has no instances).

 Void function(int a, int b) {
    //do something
    return null;
 }

Why would I like a function that always returns null?

Before the invention of generics, I didn't have an use case for Void.

With generics, there are some interesting cases. For instance, a Future<T> is a holder for the result of an asynchronous operation performed by other thread. Future.get will return the operation value (of type T), and will block until the computation is performed.

But... what if there is nothing to return? Simple: use a Future<Void>. For instance, in Google App Engine the Asyncronous Datastore Service delete operation returns a Future<Void>. When get() is invoked on that future, null is returned after the deletion is complete. One could write a similar example with Callables.

Another use case is a Map without values, i.e. a Map<T,Void>. Such a map behaves like a Set<T>, then it may be useful when there is no equivalent implementation of Set (for instance, there is no WeakHashSet, then one could use a WeakHashMap<T,Void>).

Upvotes: 63

Wug
Wug

Reputation: 13196

java.lang.Void is the boxed representation of the void type. Since you can't have an instance of type void, it's mostly there for completeness and the very rare instance where you need it for reflection.

Upvotes: 2

Daedalus
Daedalus

Reputation: 1667

Void is "an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void." (from http://docs.oracle.com/javase/6/docs/api/java/lang/Void.html)

void is a return type signifying no return.

null is the absence of value.

Upvotes: 4

Related Questions