aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes query regarding overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "query regarding overloading" Watch "query regarding overloading" New topic
Author

query regarding overloading

rahul lahiri
Greenhorn

Joined: Mar 20, 2009
Posts: 13
I've wrote the following code.



Now the output I'm getting is :-

Integer method result=7
Double method result=11.52
Double method result=7.6
Double method result=7.6

I am really confused about how java resolves the method calls.Why am I getting this output?.Please help.
Thanks in advance.



Rose Rimsky
Greenhorn

Joined: Feb 10, 2010
Posts: 25
Hi Rahul, well, it all depends on the parameters you are sending. If you have different methods with the same name, but each one receives different types of parameters, that will make the difference. In your class, first you're sending two integers as parameters to the method meth

so, the chosen method will be the one that receives two parameters type integer. All the other calls to the method meth that you did, you were sending doubles (at least one double), so the chosen method, will be the one that receives parameters type double.

Hope this help, but if I only confuse you more , let me know


Fairy tales are more than true: not because they tell us that dragons exist, but because they tell us that dragons can be beaten. – G.K. Chesterton
senthil kumar. J
Greenhorn

Joined: May 04, 2010
Posts: 5
In one shot, Java is strictly TypeCasting.

If your calling a method with some arguments, first JVM will look for that method and execute them, in-case of overloaded method JVM will look for the argument type say d.meth(5,2.6) ---> (int, double) now the compiler type casts the int value to double in order to fit to the available methods [ explicit type casting is done by default ] so your getting the below mentioned output.
rahul lahiri
Greenhorn

Joined: Mar 20, 2009
Posts: 13
Thanks for your replies.Well, I want to prepare for SCJP 1.6.Can any of you recommend any books,mock tests etc. to prepare for the exam?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
It's really simple to understand how the JVM decides which overloaded method to choose, as you see in the Java™ Language Specification. Spend a week readign that section and you will understand everything

Now you know to be very careful when overloading.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
About the SCJP books: I think I ought to move this thread tothe SCJP forum.
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

rahul lahiri wrote:Thanks for your replies.Well, I want to prepare for SCJP 1.6.Can any of you recommend any books,mock tests etc. to prepare for the exam?
Hi Rahul, good luck with the preparations!

Start with Kathy Sierra & Bert Bates book "SCJP6 study guide"
Read it cover to cover 2-3 times, practice all the examples, do all the questions at end of each chapter.
code with various permutations and combinations! Change something here and there and see how the outcome differs and find out why it behaves that way!
Finally take the Master exams along with book!
Try Devaka's "Exam Lab" FREE . It has 5 mock exams and use it as a learning tool
Go for Enthuware JQ Plus , it is only $28 and there are 13 full mock exams and do as much as you can!

And then go for the Grand Finale!

ps: In addition to the above, be an active rancher! Post your questions here, Help others and share your knowledge and expertise.
It is a give and take policy here!

All the best and take care!

SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: query regarding overloading