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

Method specific call.

 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does the below method print long int??
Even when I did a call like fn2(int,int) it called fn2(long,int) -
rather than any other method. Any reason?


 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shivani Chandna:
... Any reason? ...


JLS: 15.12.2.2 Choose the Most Specific Method
[ September 15, 2005: Message edited by: marc weber ]
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
specifically why is long preferred over double or float >? for calls like
fn2(char,char) or fn2(int,int)
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Hierachy Chart->




Whenever we pass an char parameter if it doesnt find a method with char parameter it searches for an int,because char get implictly converted to an int when neccesary.

As byte,short come lower in the hierarchy than int,method with these parameters are just ignored.(these will never be considered)

Now those method are searched whose first parameter is an int,
if not present then it searches up the hierachy thats a long,
if not then float then double.

in your case it finds two method with long
1)static void fn2(long b, int i)
2)static void fn2(long b, long i)

now it checks the second parameter it searches the same way
in the results of first parameter,
it finds the method

1)static void fn2(long b, int i)

now finally it checks that any method does not have the second parameter
of the type lower in the hierachy than it has found otherwise it generates
an ambigouty error,It is important to note that methods of byte,short are not in the scene because the parameter passed((char,char) converted to (int,int)) is above those in the hierarchy.
[ September 15, 2005: Message edited by: anand phulwani ]
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Anand - that was a great explanation.
Please can you given an example of the following statement?


now finally it checks that any method does not have the second parameter
of the type lower in the hierachy than it has found otherwise it generates
an ambigouty error




--
Shivani
[ September 16, 2005: Message edited by: Shivani Chandna ]
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anand,

That is really a nice explanation.

Regards,
Ganesh..
 
anand phulwani
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See Taking the Code which you gave,
1)removing the (long,int) method, because in this method
the second parameter is the lowest in the hierarchy,
2)and adding a (float,int) method so as to generate ambiguity

after these changes if you follow the procedure,when comparing
you find only a single valid method
1)static void fn2(long b, long i)
as the second parameter searches in the first parameter's result
it has no other option,other than to take this result,
finally when it looks that if there is any other method which has the
second parameter lower than its own,it finds one with(double,int)
which generates an ambiguoty,heres a code with the changes



which gives the error,
-------------------------------------------------------------------------
Test.java:5: reference to fn2 is ambiguous, both method fn2(long,long) in Test and method fn2(float,int) in Test match
final char c =12; fn2(c,c); //calls long int
-------------------------------------------------------------------------

An important thing to note is compiler even checks for any method defined
in your code which could generate ambiguoty


For e.g.
if i just changed the (float,int) method to (double,int)method
than this will generate ambigouty with (float,float),although we have not
called any of these method.

but if anytime,in any case,if the parameter passed to this method is (12.2f,12),it will find the method (float,float),but the second parameter it finds lower in(double,int) so generates an ambigiuty,Remember it just checks the methods for ambiguoty,whether you have called or not does not matter
Here the code for that



give error at compilation
---------------------------------------------------------------------------
Test.java:5: reference to fn2 is ambiguous, both method fn2(float,float) in Test and method fn2(double,int) in Test match
final char c =12; fn2(c,c); //calls long int
^
1 error
---------------------------------------------------------------------------

if still any doubt i will be happy to clear it ,feel free to ask.

With Luck,
Anand
 
anand phulwani
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Friends,
I really appreciate your thanks,it really makes us feel good.

Its Your care and appreciation that motivates us to write down the codes and explanation for you,i will always like you to motivate us for the same,
we will be higly gratified.
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ,
I've realized that inspite of reading and testing with function call - different kind of parameters - my concepts are not clear as yet.
Can anyone re attempt at explaining when does the function call become ambigious....

In the given code f(12,12.5,3)
so f(int,double,int)
12 = int so 1 and 2 are valid .
now 12.5 = double so,both 1 and 2 are ineligible for this.
So we choose //3 ?

--Is this how things are to be analyzed. ( // )

 
Right! We're on it! Let's get to work tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic