The compiler throws in a warning if i declare and instantiate a new type safe collection like below
List<String> list = new ArrayList(); // compiler warning
List<String> anotherList = new ArrayList<String>(); //this is normal and ok.
The list object seems to be type safe as I can’t put in any other object other than String in it. So, are there any pitfalls of using such a declaration ?
Because you could have done something like this:
But this would break the contract that a
List<String>should only containStrings. So that is why you get the warning; in general mixing raw and generic types is bad practice.