• 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

Generics and Collections

 
Ranch Hand
Posts: 31
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I have the following code:


My question is why am I allowed to put a String object in the Queue helped, in the method helpPeople, when the Queue given to the method as argument. helpedQ, is a Queue<Person>?  I understand why the compiler allows this (it only gives a warning), but shouldn't the code throw an ClassCastException or any other exception? Because in the end I will have a Queue of Person objects, with a String object in it.

Thanks in advance!
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the arguments of the helpPepole-method are the raw versions of a Queue. If you name the arguments 'Queue<Person>' then you get the expected error.

Compare with this:
reply
    Bookmark Topic Watch Topic
  • New Topic