20

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"
Josh
  • 3,264
  • 1
  • 23
  • 35

5 Answers5

61

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

Javier
  • 12,100
  • 5
  • 46
  • 57
4

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.

Daedalus
  • 1,667
  • 10
  • 12
3

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);
jedyobidan
  • 876
  • 1
  • 8
  • 14
  • 1
    it was the extra comma Thanks! i didnt realize its a cast (i thought its two args) but for some reason it does have to be casted otherwise it errors out saying `execute(void[]) is ambiguous for the type MyAsyncTask` – Josh Mar 21 '13 at 02:29
  • 2
    The safest bet in this case is actually `myAsyncTask.execute();` – battery May 30 '14 at 09:40
2

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.

Wug
  • 12,956
  • 4
  • 34
  • 54
  • `java.lang.Void` is *not* the [boxed representation](http://en.wikipedia.org/wiki/Object_type_(object-oriented_programming)#Boxing) of `void`. Calling it a boxed representation implies that you're wrapping a primitive. `void` is not a primitive, nor is it even a type, and there is no wrapping occurring. – Jeffrey Mar 21 '13 at 02:31
  • Some resources, such as http://www.idevelopment.info/data/Programming/java/miscellaneous_java/Java_Primitive_Types.html, cite `void` as being a primitive type. It's certainly not a class. It's entire purpose is to stand in for the primitive type of equivalent purpose, which is what "boxed representations" actually do. – Wug Mar 21 '13 at 02:44
  • The [Java Langauge Specification](http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.8) states that `void` is not a type. – Jeffrey Mar 21 '13 at 23:20
  • it's interesting. since when i look at the code for class `Void` i see `public static final Class TYPE = (Class) Class.getPrimitiveClass("void");` which kinds of showing that void is considered as primitive type. – Reza Aug 10 '18 at 16:20
1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Subodh Karwa
  • 2,495
  • 1
  • 15
  • 13