aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Illegal method call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Illegal method call" Watch "Illegal method call" New topic
Author

Illegal method call

Rade Koncar
Greenhorn

Joined: Sep 14, 2012
Posts: 9
Hello,

I don't understand why this won't compile:



Compiler says: f(short) in baba.jaga.Test cannot be applied to (int)

Compiler assigns 23 to variable a - this goes fine.
Why it cannot assign it to short method argument? Function is called with literal, which is a compile time constant.
So I see now problem in assigning value 23 to method argument...
Eswar Nec
Ranch Hand

Joined: May 02, 2010
Posts: 105
Hi,

You have created method for parameter type as short.

But you try to call int like f(23);

1. Try to change f(a);

2. Otherwise create one more method



Wake up! Don't let your smile be snatched away by anybody!
Regards, Eswar
Rade Koncar
Greenhorn

Joined: Sep 14, 2012
Posts: 9
Thanks on reply,

I understand that literal is int, but I don't understand why compiler assigns particular int value (23 which is valid for short) to short variable,
and won't do the same when it is method argument.
This looks like inconsistency to me, because compiler knows at compile time that this literal is small enough to fit into a short so it can safely
shorten it. Compiler does this for assignment in line 3, why not for method call in line 4...
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 367
    
    1

Rade Koncar wrote:Thanks on reply,

I understand that literal is int, but I don't understand why compiler assigns particular int value (23 which is valid for short) to short variable,
and won't do the same when it is method argument.
This looks like inconsistency to me, because compiler knows at compile time that this literal is small enough to fit into a short so it can safely
shorten it. Compiler does this for assignment in line 3, why not for method call in line 4...


Hello Rade,
According to Topic 5.3 Method Invocation Conversion of JLS: -

Method invocation contexts allow the use of one of the following:

an identity conversion (§5.1.1)
a widening primitive conversion (§5.1.2)
a widening reference conversion (§5.1.5)
a boxing conversion (§5.1.7) optionally followed by widening reference conversion
an unboxing conversion (§5.1.8) optionally followed by a widening primitive conversion.


I think you can refer the link for better explanation..

Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Rade Koncar wrote:Thanks on reply,

I understand that literal is int, but I don't understand why compiler assigns particular int value (23 which is valid for short) to short variable,
and won't do the same when it is method argument.
This looks like inconsistency to me, because compiler knows at compile time that this literal is small enough to fit into a short so it can safely
shorten it. Compiler does this for assignment in line 3, why not for method call in line 4...


When you call method f(23), then compiler thinks that 23 is an int, it cannot assign 23 as short. And since the value cannot be converged implicitely, so the compiler cannot assign int (in your case 23) to a short.
Instead if you do f((short)23), then the compiler should not complain, as you are specifying the compiler to cast it to a short


Programming is about thinking, NOT coding
Rade Koncar
Greenhorn

Joined: Sep 14, 2012
Posts: 9
Resorting to JSL was not so horrible like i feared
It states that "Method invocation conversions specifically do not include the implicit narrowing of integer constants which is part of assignment conversion"

Its just that all this conversions and overloading is a bit overwhelming, and just when I think i got it, there is some rule that breaks the logic...

Thanks for clarifications everyone...
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 367
    
    1

Rade Koncar wrote:Resorting to JSL was not so horrible like i feared

Well it's true that JLS seems horrible, when you visit it the first time, and even afterwards in some cases.
But, you can get most of the very useful answers there only and get your problem solved.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Illegal method call
 
Similar Threads
Autoboxing with shortening/widening
Problem in Scope of Reference Variable.....
overloaded var - args
Wrapper confusion
Argument Scope