Sunday, February 15, 2009

Java Generics

The problems of legacy collections:

  • There is no type safety for the collection objects.
  • Suppose we are constructing array list object to that we can add any kind of object.
  • Suppose if our requirement is to add only String objects,by mistake if we are adding any non string objects,still the compiler compiles the code without raise any problem.
  • But at the time of retrieving these objects we may get class cast exception.
Example:
ArrapList l=new ArrayList();
l.add(“laxman”);
l.add(new Integer(10));
String n1=(String)l.get(0);
String n2=(String)l.get(1);
  • Hence these is no type safety for the collection objects.
  • While retrieving the elements from the collection object we should explicitly perform type casting even though we know the type of elements present in the collection.
To resolve the above two problems Sun people provided generics concept in the 1.5 version.
Hence by using generic we can provide type safely for the collection objects and we can resolve explicit type casting problems.
i.e no need to type cast at the time of retrieving elements from the collection.

ArrayList <String > l= new ArrayList <String >();

  • This ArrayList can accept only string objects.This is declaring a generic collection object .
  • By mistake of we are adding any non string object .it will results in the compail time problem.Hence by using generics we can provide the type safety for the collection objects.
  • At the time of retrieving we can directly assign to the String type no need to problem explicit typecasting.
ArrayList <String> l=new ArrayList<String>();
l.add(“laxman”);
l.add(“scjp”);
l.add(10); // compile time error (CTE)
String n1=l.get(0);

i.e no need to perform explicit type casting so,generics are nothing but parametrized collections because by using this we can define the type parameter.

program to define ArrayList of String objects and display the contents of ArrayList by using some method:
import java.util.*;
class GenericDemo
{
public static void main(String arg[])
{
ArrayList<String> l=NEW ArrayList <String >();
l.add(“a”);
l.add(“b”);
l.add(“c”);
show(l);
}
public static void show(ArrayList<String> l)
{
System.out.println(l);
}
}

To add any kind of objects to the ArrayList we can declare generic arraylist as follows

ArrayList<Object> l=new ArrayList<Object>(); is equal to
List<Object> l=new ArrayList<Object>(); (valid)
List<Object> l=new ArrayList<String>(); (not valid)
Because polymorphism can be applicable for the base type not for the parameter type.

ArrayList<Integer> l=new ArrayList<Integer>(); (valid)
Object<Integer> l=new ArrayList<Integer>(); (valid)
Object<number> l=new ArrayList<integer>(); (not valid)
ArrayList<int> l=new ArrayList<int>(); (not valid)

Since parameter must not be primitive type .
We can’t define primitive type as the parameter of collection classes.violation leads to compail time error.
Saying unexpected type found:int required:reference


1 comments:

Anonymous said...

Can any one tell me the limitations of Legacy collections in java ?

Post a Comment