Character could (should?!) implement CharSequence?

I came across this when helping a noobie understand why this doesn’t compile:

someString.replace('A',"BB")

It got me thinking; why doesn’t the Character class implement CharSequence.
Logically a Character is a sequence of 1, thus there’s no reason it shouldn’t implement it.

Doing so would have interesting consequence when mixed with autoboxing.

Thanks to autoboxing, the uncompilable code above would start compiling. (char autoboxes to Character, which would implement CharSequence & thus meet the contract of replace(CharSequence,CharSequence))

I’m obviously not advocating such a change, as it’d introduce all sorts of potential confusion as to precisely what method was being invoked, and possible performance problems in the event of typos.

Still, it was something I hadn’t noticed or considered before.

Because a Character cannot somehow implement the required methods? :wink:

Stop being lazy and just replace the single quotes with double quotes :stuck_out_tongue:

Seems pretty trivial to implement IMO.

hehe, true.

Hm… with that reasoning, it would also not support strings with a length of one.

Anyway, I think this is genuinly overlooked by the Sun engineers, when they added CharSequence.

I was thinking more of “charAt” and “subsequence”. Those two would seem out of place :S

just as out of place as for a string of length one - would you also have problems imagining charAt for a string of length zero?

Those are scenarios for Strings. Strings can be of any size. A Character will always have a length of 1 so those two methods are useless.

The methods wouldn’t be useless since they would be used by replace() and other functions which don’t know anything about Character or String or what have you. That’s the whole point of interfaces… :slight_smile:

We’re trying to make games and we can’t even get our chars right? Go us. :stuck_out_tongue:

EDIT: The only “conflict” I can find in the String API is for replace():


replace(char oldChar, char newChar)
replace(CharSequence target, CharSequence replacement) 

I don’t think that would be a problem though? It should prioritize the char version if the input is char even if it extends CharSequence. Besides, the first one would just be an optimization of the CharSequence one specifically for chars.