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

Mixing Legacy with Generic Code

 
Abhi vijay
Ranch Hand
Posts: 509
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here even though I have mixed Generic+Legacey, NO warning is generated as the legacy code does not add anything to the ArrayList.

But when I change the program,


Here there is a Compiler Warning generated, even though I am not trying to add anything to the Arraylist. Can anyone explain?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first case is similar to:

This is OK as long as you don't add elements through l.

The second case is equivalent to:

The problem in this case is that you don't know what type of elements are already in the object pointed to by the non-generic List reference l. You could have added something that is not a String prior to the assignment in line // 1, and the compiler is warning you about that possibility.
 
Abhi vijay
Ranch Hand
Posts: 509
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ruben, I got it!
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic