Hi Mark,
A little bit of history will make you understand this question without any problem. Before
Java 5 you wrote code like this:
So you could add really everything to a List and the developer had to be very careful when working with collections. Although the list is named
myIntegers there is no way to prevent other things (like strings and dogs) to be added to that list. When you retrieved the items from the list, you always got
Objects and you had to cast them to the appropriate type (if you want to invoke a specific method). And when you cast, you have to be very, very careful (or you got a ClassCastException slapping you in the face; auwch, that hurts!
). So if we want to print the previous list, you had to write(before Java 5) code like this:
Without the instanceof operator you would get a ClassCastException (a String is not an Integer). You can spot the cons with the naked eye:
You get some boilerplate code (casting, instanceof check) that you have to do with each collection in your codeThere is no guarantee that if you have a list with integers it only contains integers...
That's why in Java 5 generics were introduced. It made the code more concise, easier to read and you have compile time protection. So in your list with integers you can only add integers. The above code in Java 5 or later versions (also using the enhanced for loop):
So like I said before: more concise and easier to read. But one important not about generics: these generic types only exists at compile time! At runtime it's just a List (like in before java 5). The compiler adds the necessary casts for you (you can see that when you "decompile" a compiled class using Jad for example.
Now Java is known for its backwards compatibility, so you can mix generic list with non-generic lists. You can pass a generic list without any problem to a method with a non-generic list. So the non-generic list from the addToList-method is exactly the same as the List from the above "before java 5" example, so you can add everything you fancy: strings, integers, cats, dogs, planes,... When you retrieve the elements, some caution is needed: in the mock question you simply print the element to the console. That's why the code compiles and runs without any problem. Every class inherits a toString-method from Object (and that's the method invoked when printing to the console).
If you wanted to invoke a specific Integer method, you needed a cast on line 15:
System.out.println(((Integer)lst.get(0)).intValue()); And that's when the ClassCastException will occur, because you don't have an Integer, but a String. If line 14 was replaced by
lst.add("0067"); you would have a compiler error because you try to add a String to a generic list which can only (if all the code uses generics
) contain integers.
Phew, what an explanation! Hope it helps!
Kind regards,
Roel