Is it because that the first declaration tells clearly that Strings can be added Whereas second list is not specifying anything ?
If its like that then what does the RHS of second declaration conveys? Doesn't it serve any purpose?
Punit Singh wrote:Can you think Prav why there is compiler warning in second line add() and why not in first line add() method?
List<String> list1 = new ArrayList(); //warning
why this compiles with warning, while this below one not?
List list2 = new ArrayList<String>();
Joined: Oct 16, 2008
Prav wrote:If its like that then what does the RHS of second declaration conveys? Doesn't it serve any purpose?
It is serving its purpose na, but in this case it is not deciding what can be added.
Reference type always restricts what you can assign to this reference type, and what can you add if reference type is collection, or what method can be called. This is the power of reference type, RHS is a small child against the power of LHS, LHS says what is possible and what is not always.