• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Compilation Fails at Line 2.

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All ,

Why is, Line 2 giving compilation Error--can not convert List<String> to List<Object>
although List<Object> is super Type ?
Please Explalin.


List<String> list1 = new ArrayList<String>();//line 1
List<Object> list2 = list1;//line 2

Thanks.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Imagine that List<Object> can contain any Object. What would happen if you could assign it a List<String>, which can only contain String. Would you be able to add Integer objects into it ? No.
 
Punya Pratap Singh
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then What is happening here,as we are assigning ArrayList<Gum>
to List Type and It is not giving any type of Error;

class Gum{}

public class TestCollection {
public static void main(String[] args) {
List<Gum> list1 = new ArrayList<Gum>();
list1.add(new Gum());
List list2 = list1; //1
list2.add(new Gum()); //2
list2.add(new Integer(9)); //3
list2.add("XYZ"); //4
System.out.println(list2.size());
}}
 
Punya Pratap Singh
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
List<Gum> list1 = new ArrayList<Gum>();

if You cahnge this line with String Type Result is same Why Is It?

Please Expalin.

Thanks.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
List list2 : This list is not a generic list, so anything can go in there.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Punya, Generics are invariant, not a covariant.
 
Punya Pratap Singh
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks For Reply..Christophe Verré

But I am wondering ,then what is happening at this line..
In what way this assignment is affecting the left hand side variable.

List list2 = list1;

Thanks.

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
List l2 is a list of anything. You can assign it any type class implementing List. Moreover, this list is not generic (it has not the List<...> notation). So it can also be assigned to any kind of generic list. It's not affecting it in any particular way. It's a good old non-generic list which was used before Java5.
 
Punya Pratap Singh
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr. Venkatasamy Thanks
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christophe Verré wrote:List l2 is a list of anything.



More expressive

Punya Pratap Singh wrote:Mr. Venkatasamy Thanks


you are welcome
 
Punya Pratap Singh
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Christophe
 
Sheriff
Posts: 22662
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll get a compiler warning about that line because it is dangerous. Your example has already shown that you now have an Integer in your List<Gum>. When you now try to iterate over it you will get a ClassCastException.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic