Originally posted by Preetha Arun:
Here we are not allowed to add anything other than String,why we are not calling this as type safe collection?
what is the difference between
List<String> li=new ArrayList();
and
List<String> li=new ArrayList<String>();?
please someone clear this..
List<String> li=new ArrayList(); is a type safe collection.
List<String> li=new ArrayList<String>(); is also a type safe collection.
There is no difference in the above two statements as far as enforcing type safety is concerned.
I had explained this in a similar post before with an example. Let me try once again.
List<String> li = new ArrayList(); is type safe but on slightly modifying the code above we can see the difference
imagine you were passed the ArrayList
al by some other method and you assign it to
li then you cannot guarantee that the ArrayList passed contains only Strings. e.g.