aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes <? super Integer> Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "<? super Integer>" Watch "<? super Integer>" New topic
Author

<? super Integer>

Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180
From JavaBeat



At Line 4 it is giving copiler error. Here List <? super Integer> means it takes Integer or its super type. But we can add only Integers to this List al. Then we know that elements from this list will have only Integers. Why it is allowing only for(Object : al) in Line 4
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16680
    
  19

At Line 4 it is giving copiler error. Here List <? super Integer> means it takes Integer or its super type. But we can add only Integers to this List al. Then we know that elements from this list will have only Integers. Why it is allowing only for(Object : al) in Line 4


Actually, no.... <? super Integer> means that it is an unknown type, but can be Integer or its super type. We can add only Integers to this List, because that is the only way to satisfy that it be all the possible unknown types.

You don't know what type of elements are actually in the list -- hence, you don't know if the elements are actually Number types. The only thing that you do know is that the elements are Object types.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
K. Tsang
Ranch Hand

Joined: Sep 13, 2007
Posts: 1219

You know what <? super Integer> means? How about <? extends Number>?


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16680
    
  19


Another way to look at it....



Just because you can only add integer types to the list using the "al" reference, doesn't mean that there are only Integers in the list. In the example above, we can add other object types into the list, using the "al0" reference instead.

Henry
Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180
if List <? super Dog> dogs=new ArrayList<Animal>();
here also we should have only Object not Animal Or Dog???
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Banu Chowdary wrote:if List <? super Dog> dogs=new ArrayList<Animal>();
here also we should have only Object not Animal Or Dog???


Anything super to Dog:



SCJP 6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16680
    
  19

Punit Singh wrote:
Banu Chowdary wrote:if List <? super Dog> dogs=new ArrayList<Animal>();
here also we should have only Object not Animal Or Dog???


Anything super to Dog:




Yes, you can assign the list to any list type of Dog or anything super to Dog... but I believe the question was refering to the elements, which is.... yes, when you iterate through the Dogs list, the element type is assumed to be Object. (You will need to cast for anything else).

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: <? super Integer>
 
Similar Threads
Adding to generic collection
Generics
Generics question List with super
doubt in generics
generics ? super Integer