aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mixing Legacy with Generic Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mixing Legacy with Generic Code" Watch "Mixing Legacy with Generic Code" New topic
Author

Mixing Legacy with Generic Code

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509


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

Joined: Dec 16, 2008
Posts: 1032
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.


All code in my posts, unless a source is explicitly mentioned, is my own.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Thanks Ruben, I got it!
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Great!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mixing Legacy with Generic Code