• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

<? super Integer>

 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know what <? super Integer> means? How about <? extends Number>?
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if List <? super Dog> dogs=new ArrayList<Animal>();
here also we should have only Object not Animal Or Dog???
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:


 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic