aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Guess the answer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Guess the answer" Watch "Guess the answer" New topic
Author

Guess the answer

Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

Hi,

just guess the answer don't compile it

String aa="hello";
StringBuffer bb= new StringBuffer("hello");

System.out.println(aa.equals(null)); // line 1
System.out.println(bb.append(null)); // line 2

which line will throw exception and why?


Regards,

Abdul Mohsin


Regards, Abdul Mohsin
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Abdul,

Good!

String aa="hello";
StringBuffer bb= new StringBuffer("hello");

System.out.println(aa.equals(null)); // line 1
System.out.println(bb.append(null)); // line 2

First line will compile and run fine, return false, any equals comparison with
null returns false.

Line 2 is ambiguous call;
[ May 01, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Well, if it does not compile the second line, then it will not throw any exceptions.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
sampath kumar yanagandla
Greenhorn

Joined: Apr 29, 2007
Posts: 13
why is it ambiguous if we pass null to append method??
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Sampath,

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 ]
sampath kumar yanagandla
Greenhorn

Joined: Apr 29, 2007
Posts: 13
I got it..
Thanks for the explanation.
 
Don't get me started about those stupid light bulbs.
 
subject: Guess the answer