File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
Anup Om
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Anup Om
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You. Now I am clear.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic