Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compilation Fails at Line 2.

 
Punya Pratap Singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • 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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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: 81
  • Mark post as helpful
  • send pies
  • 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: 81
  • Mark post as helpful
  • send pies
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List list2 : This list is not a generic list, so anything can go in there.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punya, Generics are invariant, not a covariant.
 
Punya Pratap Singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Venkatasamy Thanks
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Christophe
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic