• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Guess the answer

 
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if it does not compile the second line, then it will not throw any exceptions.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why is it ambiguous if we pass null to append method??
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it..
Thanks for the explanation.
 
I need a new interior decorator. This tiny ad just painted every room in my house purple.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic