| Author |
Generics
|
Anup Om
Ranch Hand
Joined: Dec 30, 2009
Posts: 62
|
|
Hello,
I am studying generics.
Code Snippet#1
Error Message:
cannot find symbol
symbol : method add(java.lang.String)
location: interface java.util.List<java.lang.Integer>
l.add("hello");
^
Code Snippet#2
Code Snippet#1 flags a compilation error, while Code Snippet#2 compiles and runs. I fail to understand why and difference between the two declarations of the list l.
Thanks for help in advance.
|
SCJP6
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
In the first case, you assigned an object that doesn't use generics to a reference that does -- hence the warning. Then you tried to add a string to a list that can only take integer (based on the reference) -- hence, the error.
In the second case, you assigned an object that uses generic to a reference that does not -- hence the warning. Then you tried to add a string to a list that doesn't use generics (based on the reference) -- which is perfectly fine.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Anup Om
Ranch Hand
Joined: Dec 30, 2009
Posts: 62
|
|
|
Thank You. Now I am clear.
|
 |
 |
|
|
subject: Generics
|
|
|