Relates :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
Name: dk106046 Date: 08/18/2004 Testcase : bash-2.05b$ cat Test.java import java.util.List; import java.util.LinkedList; public class Test { public static void main(String [] args) { List<Integer> list = new LinkedList<Integer>(); list.add(1); list.add(2); list.add(3); list.add("4"); for(int i : list){ System.out.println(i); }; } } On compilation, I get : bash-2.05b$ javac Test.java Test.java:12: cannot find symbol symbol : method add(java.lang.String) location: interface java.util.List<java.lang.Integer> list.add("4"); ^ 1 error That compilation fails, is as expected. However, this is a "C++ style" message. In C++, doing List<Integer> ends up creating a new datatype of a List container with Integer elements. So, when trying to stick a String into the container, the compiler would complain that the variable isnt of the right type (or in this case, that the symbol in question doesnt exist where the symbol being searched for is the container for a String). In Java, however, type instantiation does not happen : at least so claim the docs. Hence, a more intelligent message is expected along the lines of "type mismatch" or something like that. After all, isn't that the point of generics - to introduce type safety to containers? ======================================================================
|