File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Passing primitive values to methods and method 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 "Passing primitive values to methods and method overloading" Watch "Passing primitive values to methods and method overloading" New topic
Author

Passing primitive values to methods and method overloading

Sathya Srinivasan
Ranch Hand

Joined: Jan 29, 2002
Posts: 379
Hi,
I am preparing for my SCJP exam and when I was looking through JavaRanch's previously posted questions, the following problem occured to me.
Consider the following code.

The above program compiles and runs fine. But due to the widening conversions, both the methods are valid. In such a case, I would have expected the first occuring method in the program to be called.
However, the compiler seems to be kind of smart and calls the method that has the closest widening conversion. I verified this with another function with signature

and it picked that method.
Can anyone explain the reasoning behind this?


Cheers, Sathya Srinivasan - SCJP 1.2, SCWCD 1.2, SCMAD 1.0
Co-Author of Whizlabs SCMAD Certification Exam Simulator and SCMAD Exam Guide Book
Sathya Srinivasan
Ranch Hand

Joined: Jan 29, 2002
Posts: 379
As an addenda,
The output of the first piece was "DFF" and that of the second piece was "DLI".
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Sathya,
the compiler chooses the most specific method regardless of their ordered appearance within the class.
Read JLS 15.12.2.2 Choose the Most Specific Method and following for some example.
Moreover, we'd like you to read the Javaranch Naming Policy and register again.
Thank you for your cooperation.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Sathya Srinivasan
Ranch Hand

Joined: Jan 29, 2002
Posts: 379
Hi,
Thanks for the information.. Thanks for informing me about the naming policy too. I have changed it accordingly.
Mc Fly
Greenhorn

Joined: Jan 30, 2002
Posts: 3
So what do we reckon is the reasoning?
The spec seems to deal with reference types rather than primitives and conversions.
I would have thought that rather than allow a possible loss of information in the least significant digits of the long in the conversion to float the compiler would have chosen the first method
ie. output : DDD
Cheers -McFly
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing primitive values to methods and method overloading