This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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!
 
Consider Paul's rocket mass heater.
 
subject: Mixing Legacy with Generic Code
 
Similar Threads
doubt generics
Generics Generics Generics !!!
Need Help on Generecs and non-Generics
non-generic collection always compiles with warning ?
Generics warnings... I don't understand?!