File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Compilation Fails at Line 2. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Compilation Fails at Line 2." Watch "Compilation Fails at Line 2." New topic
Author

Compilation Fails at Line 2.

Punya Pratap Singh
Ranch Hand

Joined: Nov 23, 2010
Posts: 80
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Punya Pratap Singh
Ranch Hand

Joined: Nov 23, 2010
Posts: 80
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

Joined: Nov 23, 2010
Posts: 80
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

List list2 : This list is not a generic list, so anything can go in there.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Punya, Generics are invariant, not a covariant.
Punya Pratap Singh
Ranch Hand

Joined: Nov 23, 2010
Posts: 80
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Nov 23, 2010
Posts: 80
Mr. Venkatasamy Thanks
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Nov 23, 2010
Posts: 80
Thanks a lot Christophe
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compilation Fails at Line 2.