• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

query regarding overloading

 
rahul lahiri
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
senthil kumar. J
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49848
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49848
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About the SCJP books: I think I ought to move this thread tothe SCJP forum.
 
Rajeev Rnair
Ranch Hand
Posts: 310
1
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic