• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Warnings during Generic assignments

 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic