Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Upper-Bounded Wildcards

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I'm bussy with OCP and Chapter 3 there Upper-Bounded Wildcards.

There is an example like:

But what is Number?
What kind of objects can I add to this list?
It is compiling, it running...

The method they give in the book:

Or is this a mistake in the book (typo) in the declaration? (is not mentioned in the studyguide as a mistake)

I'm asking this, because the declaration is with "List<? extends Number>" and not as 'norma'l with "List<Integer>", see example Tutorialspoint below.
I have found this on Tutoiralspoint:


But I can follow this how this works but what is Number? Number does not exist in Java as far as I remember. Is not declared anywhere. I know/understand that the ? wil be converted to Object (tyep erasue)

Nico
 
Saloon Keeper
Posts: 12608
273
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Number is the superclass of all numeric primitive wrapper types in Java, and also BigInteger and BigDecimal.

Nico van de Kamp wrote:What kind of objects can I add to this list?



None. You can't add anything to a container using a variable that is declared with an upper bound. You can only get things out of it. The reason for this is that since you don't know what the exact type of the container is (this is what the question mark means), the compiler prevents you from adding integers to an object that at runtime turns out to be a container of doubles.

The super keyword does the inverse: You can add any kind of number to such a container, but you can't get any numbers out of it:

The mnemonic PECS is often used to remember this: Produces: extends; Consumer: super.

In your total() method, list is a producer of Number and is thus declared as List<? extends Number>.

In my fill() method, list is a consumer of Number and is thus declared as List<? super Number>.
 
Nico van de Kamp
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks Stephan

I'm using the book of Jeanne Boyarsky & Scott Yelikoff Java 8, because 1,5 year I have done the OCA certification.

In these book is nothing said about the Number() even not mentiond. But exist I see now. I hope that I can come further now, because it is very hard part.
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic