aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting during method invocation 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 "Casting during method invocation" Watch "Casting during method invocation" New topic
Author

Casting during method invocation

Uvnik Gupta
Ranch Hand

Joined: Jul 24, 2000
Posts: 32
Can anyone explain the output of the following code :
[pre]public class AQuestion
{
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
question.func(12, 2);
question.func(12, 2.2);
}
public void method(Object o)
{
System.out.println("Object Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public void func(int i, int j)
{
System.out.println("Int Version");
}
public void func(int i, long j)
{
System.out.println("Long Version");
}
public void func(int i, double j)
{
System.out.println("Double Long Version");
}
public void func(long i, double j)
{
System.out.println("Double Int Version");
}
}[/pre]

The O/P is :
String Version
Int Version
Double Long version
According to my understanding the O/P should have been :
Object Version
Long Version
Double Long Version.
Thanx
Uvnik
thomas
Ranch Hand

Joined: May 26, 2002
Posts: 79
uvnik:
Note that overloading is resolved by choosing the method having the most specific parameter types that can take the actual arguments.
In your case null can be assigned to both String and Object, but String is more specific than Object. So the method with the String parameter is chosen.
Likewise, for the other methods. 12 and 2 are integers. 2.2 is double.
Uvnik Gupta
Ranch Hand

Joined: Jul 24, 2000
Posts: 32
Thanx Thomas. I just figured out one more problem with my code. I had the messages for "Double Long version" ans "Double Int Version" written wrongly which added to my confusion.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting during method invocation
 
Similar Threads
null arg in overloaded method
overload question
null as parameter
run time error
Which overloaded method will be called if i am passing null as argument?