Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null

 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


s.append(null) gives me some ambiguous error? can anyone explain why?
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but does in this case..stringbuffer doesnt have 2 append methods
 
Henry Wong
author
Marshal
Pie
Posts: 21408
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)"
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator






this is the order i think in which the methods are called......what does this mean? isnt the "null" be returned?
 
Henry Wong
author
Marshal
Pie
Posts: 21408
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oooooooooooooh.........since string and stringubuffer are more specific than object....there is an ambguity error
 
Henry Wong
author
Marshal
Pie
Posts: 21408
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it means compile is considering only 2 method.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well the compiler shows that ambiguity is between stringbuffer and string methods
 
Henry Wong
author
Marshal
Pie
Posts: 21408
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry for your time....understood a lot
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


does this make sense? not to me
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm I see, thanks Ankur its more clear now.
 
Henry Wong
author
Marshal
Pie
Posts: 21408
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we are becoming better java players...any employers seeing this?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic