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 Method with null as arg Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method with null as arg" Watch "Method with null as arg" New topic
Author

Method with null as arg

Shubha Kirani
Greenhorn

Joined: Jun 13, 2007
Posts: 12
In the below code, compiler error with message 'method is ambiguous'.

If I comment the method with StringBuffer as argument it compiles and gives "Object" as output. Can somebody explain why this happen ?

Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Even if you comment the method that takes in a String argument it would work. The reason is that null is a valid argument for Object, String and StringBuffer. But the method with closest match is chosen over other methods for execution. Now when we have methods that accept Object type and String type of argument. String is more specific so the compiler knows that String method should be used. But when you have two or more completely different things(no where related in the class hierarchy) as the argumnet it is impossible for the compiler to determine which method should be called. Hence the error.
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by Anupam Sinha:
But when you have two or more completely different things(no where related in the class hierarchy) as the argumnet it is impossible for the compiler to determine which method should be called. Hence the error.


Even if the classes are related in the class hierarchy, it may be impossible for the compiler to decide which function should be called.

Shubha Kirani
Greenhorn

Joined: Jun 13, 2007
Posts: 12
Thanks Anupam and Manfred for your replies. I'm understanding Java better now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method with null as arg