简体   繁体   中英

Java Generics - What is this syntax for?

What does this part of the code below <String, Void, Bitmap> mean? I don't even know what this syntax is even called.

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

}



Here is the original code (Found from here: http://developer.android.com/guide/components/processes-and-threads.html ):

public void onClick(View v) {
    new DownloadImageTask().execute("http://example.com/image.png");
}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    /** The system calls this to perform work in a worker thread and
      * delivers it the parameters given to AsyncTask.execute() */
    protected Bitmap doInBackground(String... urls) {
        return loadImageFromNetwork(urls[0]);
    }

    /** The system calls this to perform work in the UI thread and delivers
      * the result from doInBackground() */
    protected void onPostExecute(Bitmap result) {
        mImageView.setImageBitmap(result);
    }
}
AsyncTask<String, Void, Bitmap>

Tells AsyncTask is described by 3 distinct types, String as first parameter, Void as second parameter and Bitmap as third parameter, when you use AsyncTask.

This is called Generics in java, introduced from Java5 onwards. Please read this tutorial to understand more about Generics. Here is javadoc on how android AsyncTasktask uses generics.

Update: From AsyncTask javadoc

1) Params, the type of the parameters sent to the task upon execution.
2) Progress, the type of the progress units published during the background computation.
3) Result, the type of the result of the background computation.

It's called Generics . Here is the details from the AsyncTask manual :

The three types used by an asynchronous task are the following:

Params , the type of the parameters sent to the task upon execution.

Progress , the type of the progress units published during the background computation.

Result , the type of the result of the background computation. Not all types are always used by an asynchronous task.

To mark a type as unused, simply use the type Void:

So AsyncTask<String, Void, Bitmap> means that, AsyncTask --DownloadImageTask accepts the param as String , Progress type is unused and returns the result as Bitmap

AsyncTask is a generic class. You should look at the generics tutorial to understand the syntax and semantics of generics.

If you look at the AsyncTask docs you will see what each of those parameters means.

  • The first is marked as "params" and is the type that your doInBackground method accepts.
  • The second is the type that is used to denote progress, as taken in the onProgressUpdate method.
  • The third is the resulting type of the task, the type that is returned from doInBackground and received by onPostExecute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM