Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty Java generics

I realize this issue is somewhat trivial, but I'm interested in knowing the "correct" answer.

I'm trying to extend android.os.AsyncTask<Params, Progress, Result>,
but my task doesn't require any parameters.

Given that I have to overload protected Result doInBackground(Params... params),
what is the "best" way to pass in no parameters?

Currently, I'm using Object as the Params type and starting the task with:
new MyAsyncTask().execute( (Object)null );
This is totally functional, but I'm new to Java and don't know if this is a good way of accomplishing what I'm trying to do.

Obviously I can't put something like void or null in for the Params field, but if I use a wildcard I'm unsure of what to do about the arguments to doInBackground(Params... params).

Any suggestions?

like image 372
koschei Avatar asked Dec 27 '10 21:12

koschei


People also ask

What does empty <> mean in Java?

The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .

What is meant by erasure in generics?

Type erasure is a process in which compiler replaces a generic parameter with actual class or bridge method. In type erasure, compiler ensures that no extra classes are created and there is no runtime overhead.

Can generics be primitive?

6. Generics and Primitive Data Types. One restriction of generics in Java is that the type parameter cannot be a primitive type.

How do I restrict a generic type in Java?

Whenever you want to restrict the type parameter to subtypes of a particular class you can use the bounded type parameter. If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class.


1 Answers

Use Void (note the capital V) for any data types that you do not need. Be sure to use the @Override annotation, so the compiler will catch any problems. Here is a sample application demonstrating this.

like image 172
CommonsWare Avatar answered Sep 27 '22 17:09

CommonsWare