1. Array objects are by default type-safe. i.e if we declare String array, we can insert only String Objects.
  2. By mistake if we are trying to insert any other elements we will get compile time error.
    String [] s = new String[100];
    s[0] = "Java";
    s[1] = 10;//Compile time error.
  3. But Collection objects are not type safe by default.
  4. If our requirement is to add only String objects to the ArrayList,By mistake if we are trying to insert any other element we won’t get any compile time error.
    ArrayList l = new ArrayList();
    l.add(new Integer(10)); //No Compile Time Error
  5. While retrieving Array elements there is no need to perform typecasting.
    String name = s[0];//No typecasting required here.
  6. But while retrieving the elements from ArrayList compulsory we should perform typecasting.
    String name = l.get(0); //Compile Time Error
    String name = (String)l.get(0);

To resolve the above 2 problems (type safety, typecasting) sun people introduced generics concept in the 1.5 version.

  1. If we want to create ArrayList object to hold any String Objects we have to define as follows.
    ArrayList<String> l = new ArrayList<String>();
  2. For the above ArrayList,we have to add only String objects. By mistake if we r trying to add any other type we will get compile time error.
    l.add(new Integer(10));//Compile Time Error: 
    //can't find the symbol add(Integer)
  3. At the time of retrieval no need to perform any typecasting.
    String name = l.get(0);// No typecasting is required.
  4. Hence by using generics we can provide type safety and we can resolve typecasting problems
  5. By using generics we can define parameter for the collection. These parametrized collection classes are nothing but “Generic collection classes“.
  6. Polymorphism concept is not applicable for the parameter type but applicable for base type.
    List<String> l = new ArrayList<String>()
    List<object> l = new ArrayList<String>();
    /*Compile Time Error: Incompatible Types
    Required :List<Object>
  7. The type parameter must be object type(any class or interface name).We can’t apply generic concept for primitive data type.
    ArrayList<int> l = new ArrayList<int>();
    /*Compile Time Error: unexpected type found : int
    Required : Integer