File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Warnings during Generic assignments 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 "Warnings during Generic assignments" Watch "Warnings during Generic assignments" New topic

Warnings during Generic assignments

Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 182

From Java Beat

I am getting warning at line 2. But it is given that

ArrayList b = a; This assignment assigns a typesafe arraylist in to a non-typesafe
arraylist, So this assignment causes warning during compilation.

Which is true??
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137

The explanation given is right only!
As fas as i know, it shouldnt give any warnings at line 2.
What warning are you getting at line 2?

Thanks,<br />Srilatha M
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Only line 2 will give you warning,

As it is assigning raw collection to parameterized collection, and raw collection could be of any type, Bus, Truck, Train that could claim ClassCastException at runtime when it will try to return Bus and it is containing Truck, so only line 2 will give warning.

Line 1 is safe as you are assigning to a raw ArrayList that could take any type of ArrayList as no parameter bound, so no ClassCastException as it will always return Object.

I agree. Here's the link:
subject: Warnings during Generic assignments
It's not a secret anymore!