Josh Josh - 4 months ago 26
Java Question

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 because I'm looking at sample android code where they used Void but eclipse is breaking on it (it says
Void cannot be resolved to a variable
).

mycode 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"

Answer

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);
Comments