• 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

Specific method call

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How this code gives an Error while the one below doesnot ?
How does specific method call works . Is it different for short,byte and char types ?
Code 1 :


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


Here you call m1 with two arguments of type int. The compiler cannot automatically convert an int to a short (that would be loss of precision), you would have to add an explicit cast to do that.

In code1 you need to cast the int, because you do not have an appropriate method signature --> compiler recognizes that and gives error.

In code2 you have a method signature that accepts two arguments of type int, so it is no problem there.
 
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
Lavjeet: I think No�l probably answered your question , but I'm adding this in response to your private message...

Literal integral values are automatically considered type int. Therefore, when you call a method and provide literal integral values as arguments, then the compiler will look for the method that accepts ints as arguments.

Specifically, when you call m1(10, 10), the compiler looks for a method defined as m1(int x, int y).

If the compiler can't find such a method, then it looks for a method that accepts primitive arguments that are wider than what you provided, and automatically converts the ints you passed to the wider type. This is "method-call conversion," and presents no problem because widening a value doesn't lose any information.

However, the compiler will not automatically convert the arguments you provided to a narrower type (byte, char, or short), because that could result in a loss of information and give unexpected results.

So when you call m1(10, 10) with ints, and the compiler can only find m1() defined to take narrower arguments (like bytes or shorts), then it will give an error saying that it cannot find the requested method.
 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic