2

How does the datatype checking in a generic collection happen ? For example, take a look at the ArrayList below :

ArrayList<String> r1 = new ArrayList<String>(); 
r1.add("4");
r1.add("1");
r1.add("2");
r1.add("3");

How is the ArrayList<String> linked to the method add() ? If I were to create my own class which is not a collection, how do i tie the generics parameter to make sure strict type checking occurs ?

Another example I observed in Android with the AsyncTask class :

private class SomeAsyncTask extends AsyncTask<String, Integer, Long> {

where String refers to the input, Integer to the progress and Long to the response. Where is this mapping done ? How do i create my own generic class and where do i define this mapping ?

YD8877
  • 10,401
  • 20
  • 64
  • 92
  • 1
    Addition to the answers: the type checking is only done by the compiler. No check is done at runtime. – JB Nizet Aug 06 '12 at 13:34

3 Answers3

3

It is of my opinion that Generics is too much of a broad subject to be covered decently as a response. I would recommend you start by taking a look at Oracle's tutorial (here) for all the information you should need.

That being said, as a quick answer, the .add() method, as per android documentation, takes a generic parameter E, which is the same parameter specified when the list is created, in your case this being a string.

Secondly, to create your own generic class, it is as simple as doing something like so:

public MyClass<T>
{
    private T myT;

    public void set(T t)
    {
        this.myT = t;
    }

    public T get()
    {
       return this.myT;
    }
}

In the case above, T refers to the generic parameter. The parameter is set when you instantiate your class, such as ... new MyClass<String>();.

npinti
  • 51,780
  • 5
  • 72
  • 96
3

If you want to create a generic class that has a doSomething method, you could write it:

class GenericClass<E> {
    public void doSomething(E aThing) {
       //do something with aThing
    }
}

You can now use it "like" an arraylist:

GenericClass<String> myGeneric = new GenericClass<String>();
myGeneric.doSomething("abc");

GenericClass<Integer> myGeneric2 = new GenericClass<Integer>();
myGeneric.doSomething(123);

Now if you want to know more about how ArrayList is implemented, the best thing to do is to check the code.

assylias
  • 321,522
  • 82
  • 660
  • 783
2

First question

Take a look at the Generic Types Tutorial for Java. An example might clarify this:

public class Tuple<X, Y> {

   X first;
   Y second;

   public Tuple(X first, Y second) {
      this.first = first;    
      this.second = second;
   }
}

When you instantiate this class, you could do it for instance with the following line:

Tuple<String, Integer> tuple = new Tuple<String, Integer>("Hello", 1);

and the type parameter check is done here during compile time, since the constructor expects now String and an Integer as parameter (in this order).

Second question

I hope the tuple example already made clear how the mapping can be accomplished: with different generic type parameter names.

The same is done in the case of AsyncTask in the type parameter definition, different names are used, for instance: AsyncTask<A, B, C> and thus they can be discriminated when applying each of them.

For that take a look at the source code of AsyncTask, where the class is defined as:

public abstract class AsyncTask<Params, Progress, Result>

The type parameter name does not have to be a single letter, it can also be a whole name.

Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
  • I instantiated ArrayList() with no parameters. But how does the ArrayList class know that the type checking should be done in the add() method. What if i have a method that converts a string to an object and I dont require type checking ? Where is this checking done ? – YD8877 Aug 06 '12 at 13:51
  • @WarDoGG Do you mean invoking `new ArrayList()`, thus omitting the generic type parameter? The result is a list with the raw type. There is **no type safety with raw types**, hence no checking is done. You can read more on this topic in the answer to the following question [What is a raw type and why shouldn't we use it?](http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it). – Konrad Reiche Aug 06 '12 at 14:06
  • @WarDoGG Regarding your method which converts a `String` to an `Object`: I cannot answer this in general without knowing the use case or what you want to accomplish. – Konrad Reiche Aug 06 '12 at 14:11