• 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

Why doesn't these assignments work when casting with Generics?

 
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
List<Object> objectList = new ArrayList();
List<String> stringList = new ArrayList();


objectList = stringList; // This doesn't work

objectList = (List<Object>)stringList; //this doesn't work either
___________________________________________________________________________
Can anyone explain why these two assignments won't work and what needs to be done in order to perform this assignment?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ronnie Phelps wrote:List<Object> objectList = new ArrayList();
List<String> stringList = new ArrayList();



You actually should *not* be doing this -- assigning a non generics collection to a generics one. This is allowed in Java in order to maintain backward compatibility with Java 1.4 and eariler.

Ronnie Phelps wrote:objectList = stringList; // This doesn't work



It doesn't work because it is not a valid assignment. A list of strings can only hold string objects. If you are allowed to assign it to a reference of a list of objects, then you will be able to add any object type list, that another reference believes it to only hold strings.

Ronnie Phelps wrote:
objectList = (List<Object>)stringList; //this doesn't work either



Same reason as previous. Regardless, if you really want to break generics (by casting), then you can do it by casting it to a List that doesn't use generics first.

Henry
 
Ronnie Phelps
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for your timely response Henry. I also noticed that the first assignment will work if I change the signature of the objectList to be

List<? extends Object> objectList = new ArrayList();


objectList = stringList; // does work now



I understand that it doesn't make since to use generics since we are extending Object. The types in this posting are String and Object in order to simplify my posting. I'm actually using other types in my code. Thanks again.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic