Sooraj Rajagopalan wrote:The List, maps, set, SortedSet in java (also known as collections) are all interfaces. And everytime you use the collections, the Interface is implemented. In this example, now that you have narrowed it down to <String>, the implementation of the interface "List", stores String.
Now consider the following
The same thing happens, which is the implementation of the interface. Only that list1 stores Lists(which store String).
Hope it helps than making you all the more confused. cheers!!
J. Kevin Robbins wrote:Those are called Generics and it makes sure that you can't store anything else in that collection except what you've declared, in your case, a String. The advantage of this is that it turns a possible runtime error into a compilation error. Without the generic declaration in there, the code would compile and run and wouldn't blow up until your code tried to store say an Integer. By declaring the type with a Generic, the compiler can catch the error before it becomes a runtime problem.
Stephan van Hulst wrote:If I wrote a class named ListIterator, how would your application know to use the standard ListIterator and not another one?