• 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

Generics

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


answer

d)Error.Because in the declaration we have specified any super type of Integer,But in for each loop we are specifying Number,We cannot restrict whenever we are using lowerbound.So accepatble thing in line 4 is for (Object no:al).
===========================================================================
Number is also a super type of Integer. Then why it is not acceptable?
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi dolly,

it's because Object is also a super type of Number. So in your code, one can easily put an Object in your arraylist like following:

al.add(new Object());

in that case your for loop will cry....


ok...
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, cowboys,

this answer is not quite correct.

In a
List <? super Integer> al = new ArrayList<Number>();

you can not add an Object. Would be a compiler error.
You can't even add a Number. Only Integers.

You may assign al to an ArrayList<Integer>, <Number> or <Object>, but in all these lists you can add only Integers.
Because al's type is unknown. Known is only that the smallest it can store is Integer. Therefore the highest you can put in must also be Integer, otherwise you may get class cast exceptions. E.g. in the case when you assign it to an ArrayList<Integer>, which is perfectly legal, you also may only store Integers in it.

Therefore lines 2 and 3 are legal. Integers can be stored properly.



Things change when you look on the types, that you can get out of it:
They're of type Object, because the list is guaranteed to contain any type from Integer upward.
That could be Integer, Number or Object.

To be type safe, the collection must return type Object as well. Integer or Number are too small, because there could be Objects in it.
OK, not in this one, as variable al refers to an object of ArrayList<Number>, but al could easily be re-referenced to another object with type ArrayList<Object>.

Therefore the "running variable" in the for-each loop "no" must also be of type Object. Every other type - like Number in this example - would cause the compiler complaining about the programmer.
Therefore answer d.

And: yes, it is complicated.


Yours,
Bu.
 
We're being followed by intergalactic spies! Quick! Take this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic