-
Enhancement
-
Resolution: Fixed
-
P4
-
5.0
-
b114
-
x86
-
windows_xp
-
Verified
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?
======================================================================
- relates to
-
JDK-6492019 Overhaul compiler diagnostics
-
- Resolved
-
-
JDK-6822637 ResolveError hierarchy needs to be refactored
-
- Closed
-
-
JDK-6805143 javac doesn't compile static method invocation in UTF_8_new
-
- Closed
-
-
JDK-6768932 Add support for multiline diagnostics
-
- Closed
-
-
JDK-6769027 Source line should be displayed immediately after the first diagnostic line
-
- Closed
-
-
JDK-7098660 Write better overload resolution/inference tests
-
- Closed
-
(1 relates to)