Don Redd wrote:
the code below is working fine!
Don Redd wrote:
case2:
but,why is below code giving compile time error saying ambiguous call as both methods match invocation
Don Redd wrote:Hi
i am little confused with below pieces of code.
case1:
the code below is working fine!
case2:
but,why is below code giving compile time error saying ambiguous call as both methods match invocation
Can someone explain whats going on in case 2,
Thanks,
Don,Redd...
Don Redd wrote:
Java says that in order to support pre java5 functionality/code , compiler gives least priority to widening, boxing and var-args,
and I am happy of compiler behavior with widening and boxing. but not with var-args
Compiler is breaking the promise at line no 5.
Can someone help me understanding this behavior.
gurpeet singh wrote:
no there is no breaking of promise. widening wins over boxing, widening wins over var-args, boxing wins over var-args. in short var-args is always the looser. here there is no such case. here the problem is of most specific method. in this case the most specific method is the one taking int... i as the parameter. had you read the corey's most specific method (i gave link earlier) you would have been able to answer this question. nonetheless, the most specific method is the one with var-args as parameter as it can take 0 or more ints, whereas the method with int i as parameter can take just one int.
Don Redd wrote:Yes, its printing Primitive on my personal machine at home, have to check tomorrow why my office workstation confused me ,anyways thanks Gurpeet !!!
Peter Piddle - Skype(peter.piddle)
Don Redd wrote:if you try to fit this arguments (int a, long b, long c) in to this set of arguments (long a, int b, int c) second and third arguments conflict because long cannot be fit into int
now the other way
if you try to fit this arguments (long a, int b, int c) in to this set (int a, long b, long c) of arguments first argument conflicts because long cannot be fit into int
so there is no specific or generic method here, just ambiguity
Don Redd wrote: you can replace word 'you' with 'compiler' , the above checks are done by compiler to find out most specific method for your invocation, but it could not find any specific method!!!
Pritish Chakraborty wrote:When you pass whole numbers within the range of an integer, they are always treated as integers.
Basically you are trying to pass three integers into methods, none of which take three integers.
This is not C++, they won't be implicitly converted to long unless you apply the L suffix or a cast.
When the number is larger/smaller than an integer, the L suffix becomes necessary or the compiler gives an error.
Pritish Chakraborty wrote:The answer is already known...
No, the compiler will not convert that one integer into a long for you. You have to do it yourself!
What is the result of compiling and executing the above code?
Prints "In doIt(int, int, long)"
Don Redd wrote:Hi Shalini,
Going back to your first post, you mentioned one method is taking two int's and it shall be specific.
the meaning of specific is little different than what you are thinking.
lets say you have two methods
and when you invoke doo(1,2); then compiler cannot find method of signature doo(int,int) but still it compiles and executes method doo(float f,double d) because of the two methods given one above this one specific.( applying same logic mentioned before)
in below case the compiler fails to find specific method( applying same logic mentioned before) for the invocation doo(1,2) . and gives compiler error.......
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|