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

Illegal method call

 
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Rade Koncar
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Rade Koncar
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic