This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
why is it ambiguous if we pass null to append method??
Joined: Feb 28, 2007
Welcome to the JavaRanch!
Originally posted by sampath kumar yanagandla: why is it ambiguous if we pass null to append method??
StringBuffer has following methods where null can be ok to pass: 1- append(char char) 2- append(CharSequence s) 3- append(Object obj) 4- append(String args) 5- append(StringBuffer args)
See the following code, where I have overloaded append methods with same argument what StringBuffer's append() requires
Take care of order in which methods are defined in the class regarding error message. No method is more specific than the other.
Suppose you have only two overloaded methods, one takes Object and another takes StringBuffer, that is fine StringBuffer will be called because it is more specific than the Object one. What that can be passed to StringBuffer can be passed to Object but vice versa in not true.
OK now add one more method which takes String, now the call to append is ambiguous, all three can be passed null and no one is specific than the other. If compiler chooses String, it can be more specific than the Object but not than the StringBuffer, because String can't be assigned to StringBuffer.
The same ambiguity is caused when you have a method that takes char, it is also not specific than the other one.
You can try using own example:
Comment the ambiguous methods one by one and see the method call. [ May 03, 2007: Message edited by: Chandra Bhatt ]