Originally posted by Saran:
List l=new List();
l.add("hi");
l.add("hello");
String s=l.get(0);////here
but what is the return type here by default?i know i will need a cast to String for making the code correct.
List l = new ArrayList();
//List l is not type safe so by default it is
List<?> l;
But don't treat it like
List<Object> l;
The difference between
List<?> and List<Object>:
List<?> l = new ArrayList<Animal>(); //OK
l = new ArrayList<String>(); //OK
l = new ArrayList<Integer>(); //OK
But
List<Object> l;
l = new ArrayList<Object>(); //OK
l = new ArrayList<String>(); //ERROR
l = new ArrayList<Integer>(); //ERROR
l = new ArrayList(); //OK with warning, unsafe operation
You got the difference, List<Object> l can only hold reference of a List(Object that IS-A List of course) and parameterized with Object;
In your example what is returned is Object, so you have to cast from Object to String because you know it is String ultimately.
Regards,
cmbhatt
[ April 16, 2007: Message edited by: Chandra Bhatt ]