• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question regarding overloaded methods vs generics

 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i just finished reading the generics and colletion chapter of the SCJP study guide. after reading that i figured that without using overloaded methods you can use generics to make one method accept many different types. am i correct? and the other thing is if you declare a method such as

public <T extends Number> Integer test(T in){

return in;
}
and if i pass a Float or a Double will it convert it to a Integer and return the value?


thank you.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not being a complete expert, I can only suggest you take my opinion with a pinch of salt.

I believe that when you pass in a Float or Double it won't cause a problem, but when you try to return that value you passed in, it will cause a comiler failure, as Float or Double ain't an Integer.

if you pass in a Float, you should imagine the method like this



you cannot return a Float when an Integer is expected

Sok
 
Normally trees don't drive trucks. Does this tiny ad have a license?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic