| Author |
null
|
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
s.append(null) gives me some ambiguous error? can anyone explain why?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
|
http://faq.javaranch.com/java/ScjpFaq#mostSpecific
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
|
but does in this case..stringbuffer doesnt have 2 append methods
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Ankur kothari wrote:but does in this case..stringbuffer doesnt have 2 append methods
Just did a count. StringBuffer doesn't have 2 append() methods. It has 13 append() methods!! ... It shouldn't be too hard to figure out which two (or more) are ambiguous.
Henry
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
|
oh so there is an ambiguous call between append(String) and append(Object)....but since string is more specific...so isnt append(String) called?
|
 |
Minhaj Mehmood
Ranch Hand
Joined: Jan 22, 2007
Posts: 400
|
|
Ankur kothari wrote:
s.append(null) gives me some ambiguous error? can anyone explain why?
at above line compiler is confuse to use 1 of the following method
1. append(StringBuffer sb)
2. append(String str)
but I'm not sure why compiler is not considering the method "append(Object obj)"
|
SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
this is the order i think in which the methods are called......what does this mean? isnt the "null" be returned?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Ankur kothari wrote:oh so there is an ambiguous call between append(String) and append(Object)....but since string is more specific...so isnt append(String) called?
What about append() with charsequence, string buffer, or char array? Can't those also take null? Give those five append() methods, which one is the most specific?
Henry
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
|
oooooooooooooh.........since string and stringubuffer are more specific than object....there is an ambguity error
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Ankur kothari wrote:
[code deleted]
this is the order i think in which the methods are called......what does this mean? isnt the "null" be returned?
Isn't this a compile error? Meaning that you haven't run it yet? What does the order of the method being called have to do with a compile error?
Henry
|
 |
Minhaj Mehmood
Ranch Hand
Joined: Jan 22, 2007
Posts: 400
|
|
Henry Wong wrote:
What about append() with charsequence, string buffer, or char array? Can't those also take null? Give those five append() methods, which one is the most specific?
Henry
No,when compiling the above code I'm getting
reference to append is ambiguous, both method append(java.lang.String) in java.lang.AbstractStringBuilder and method append(java.lang.StringBuffer) in java.lang.AbstractStringBuilder match
|
 |
Minhaj Mehmood
Ranch Hand
Joined: Jan 22, 2007
Posts: 400
|
|
|
it means compile is considering only 2 method.
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
i have compiled it...got that ambiguity error....i was just trying to find out what happens if the call goes to object argument method...but later understood that string and stringubuffer are more specific than it...
sorry
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
|
well the compiler shows that ambiguity is between stringbuffer and string methods
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Ankur kothari wrote:well the compiler shows that ambiguity is between stringbuffer and string methods
Yes, because that is what the compiler found. It doesn't mean there are no more ambiguous methods. Both String and char array subclass from Object, that is just as ambiguous. etc.
Henry
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
|
Thanks Henry for your time....understood a lot
|
 |
Minhaj Mehmood
Ranch Hand
Joined: Jan 22, 2007
Posts: 400
|
|
Henry Wong wrote:
Ankur kothari wrote:well the compiler shows that ambiguity is between stringbuffer and string methods
Yes, because that is what the compiler found. It doesn't mean there are no more ambiguous methods. Both String and char array subclass from Object, that is just as ambiguous. etc.
Henry
Ok! but its still not clear to me, the main question is:
why compiler dont complaining ambiguity between all of the 5 methods??
append(char[] str)
append(CharSequence s)
append(Object obj)
append(String str)
append(StringBuffer sb)
rather than only
append(String str)
append(StringBuffer sb)
in fact all of 5 are applicable to take null as argument!!
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
does this make sense? not to me
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
|
hey charsequence is an interface and implemented by stringbuffer...i checked using netbeans......and even string implements it too...so they are more specific than charsequence
|
 |
Minhaj Mehmood
Ranch Hand
Joined: Jan 22, 2007
Posts: 400
|
|
Hmm I see, thanks Ankur its more clear now.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Minhaj kaimkhani wrote:Ok! but its still not clear to me, the main question is:
why compiler dont complaining ambiguity between all of the 5 methods??
This is why you compile. Fix the first (or maybe the second error) found. And recompile.
Compile errors may hide other compilation errors. Compile errors may trigger compilation errors, where there is none. The compiler is far from perfect -- fix the first error, and recompile.
Henry
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
|
we are becoming better java players...any employers seeing this?
|
 |
 |
|
|
subject: null
|
|
|