Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Warnings during Generic assignments

 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic