• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

generics

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


Why does it give compilation error?
 
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Dishi,
Compiler generates error because you cannot widen and then box..

Actually what i believe is short is Autoboxed to Short but that Short type is not subclass of Integer,
so compiler says it cant find the appropriate method...

Refer K&B, chapter3 (page 248-254)...

HTH
 
Ranch Hand
Posts: 37
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dishi

as it is Treeset. So, before inserting any new object (in this case number) it will compare with the previous object and then will insert.
The previous object is of Integer type and we are inserting short type which will ultimately autobox to Short. Comparison is ok. but
at the time of insertion Short into generic type of Integer will not allow as Integer and Short are are siblings under wrapper class and can't be casted to each-other.

Thanks,
Prakash
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Prakash Mahto,
Actually it does not matter whether you use TreeSet or not..
variable number is correctly autoboxed to Short but
all wrapper classes are peers to one another so the compiler complains.

The modified code still gives the same error...
 
What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic