File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question regarding overloaded methods vs generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question regarding overloaded methods vs generics" Watch "question regarding overloaded methods vs generics" New topic

question regarding overloaded methods vs generics

Dinuka Arsakularatne
Ranch Hand

Joined: Aug 12, 2006
Posts: 198
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.

if who i am is what i have, and what i have is lost, then who am i?<br /> <br />SCJP 5.0<br />SCWCD 1.4<br />SCBCD preparing
Stephen O'Kane

Joined: Aug 17, 2005
Posts: 26
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

I agree. Here's the link:
subject: question regarding overloaded methods vs generics
It's not a secret anymore!