Search code examples
javaandroidnullvoid

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"

Solution

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