This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

unable to assign a list of string to a list of object  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unable to assign a list of string to a list of object.

List<String> listString = new ArrayList<String>();
List<Object> listObject = listString; // type mismatch: cannot convert from List<String> to List<Object>
 
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is how Generics work. You cannot cast or convert a generic type, only the base type.

So you can assign an ArrayList<String> to a List<String> reference variable, but you can never assign an object with one generic type to an object with another generic type, no matter what inheritance there is.

This is because generics don't exist at runtime, and if the compiler would allow you to do that, your program could very well crash.

It is explained well in K&B's Study Guide.

Imagine you put your ArrayList<String> into a List<Object>. Then what you do is you put an Object into your List<Object> reference. But your List<Object> is still, for all intents and purposes an ArrayList<String>! So what you're doing is you're putting an Object into a String ArrayList and your compiler can't catch it because you're using a List<Object> reference. And at runtime, generics don't exist! Next thing you know, your program explodes and your users are full of gory bits and bloodspatter.

Hope I explained that well enough.
 
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, You can't do this.

For example,
A List<Dog> is not a List<Animal>. Consider what you can do with a List<Animal> - you can add any animal to it... including a cat. Now, can you logically add a cat to a litter of puppies? Absolutely not.

So in your case, you can't assign a ArrayList<String> to a List<Object>
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another point, there are ways to assign using wildcards in java. If you want, you can Google it!
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because in case of List<Object>......it can contain anything which is an object which can be or not be same as a string object.......it may be down to any herarchy of object so, compiler will be comfused

Like
Object-->Animal-->Dog
--->Cat
anything.....

but if you really wanna do that....then try this......

List<? extends Object> =listString
this says anything which is a subclass of object can refer to String Object.......
As String also extends Object so it is also a subclass of Object,so both are at same level of herarchy......

or also try this
List<? super String>=listString.





 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shanky sohar wrote:
or also try this
List<? super Object>=listString.


That won't work. You can't assign an ArrayList<String> to a List<? super Object>

It should be List<? super String>, or List<? extends String>.

Course, in the case of String, using List<? extends String> would be kind of silly.

But with this kind of thing you have to be careful that what you're doing in your method is very safe.
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These kind of questions certainly bring out something which you can find interesting.

Can anyone comment on why



is a legal declaration why not just use List<Object> ?

Only possibility of allowing this I can think is maybe if language decides to add a SuperObject at the topmost hierarchy.




 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a problem in that............

List<? super Object> s = null;
this means any class that is the subclass of object can have null as a refernce.....
this is as simple as saying.
Cat c=null;
Dog d=null;
Integer i=null;
Float f=null;
Character g=null;
Long l=null;

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!