File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Specific method call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Specific method call" Watch "Specific method call" New topic
Author

Specific method call

Sim Kim
Ranch Hand

Joined: Aug 06, 2004
Posts: 268
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 :
No�l Verdurmen
Ranch Hand

Joined: Jul 28, 2004
Posts: 33


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.


No�l
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Specific method call
 
Similar Threads
var args method
More specific method
Execution
Question about polymorphism and casting
static method