Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Android OS or Java, what's the difference between a character sequence and a string?

For the API (Android SDK API version 8) functions whose definitions list character sequence parameters, I can feed the function String instead. Best part is that they work the same way, no hassle, no warnings.

Is there a difference between the two types? And more importantly, is there an inherent danger if I send the function a String instead of a character sequence???

Thanks for any clarifications!!! :D

like image 913
K-RAN Avatar asked Jun 06 '26 06:06

K-RAN


1 Answers

Is there a difference between the two types?

Yes. String is a class, and CharSequence is an interface. If a method accepts a CharSequence, it only requires the argument to implement the methods in the interface, which gives a greater flexibility, than if it would accept only a String.

The following API classes implement CharSequence: CharBuffer, Segment, String, StringBuffer, StringBuilder. That is, a String is a CharSequence, and can thus be passed to a method that accepts a CharSequence.

And more importantly, is there an inherent danger if I send the function a String instead of a character sequence???

No, there is no danger. String properly implements CharSequence, and that's all the method will require to function properly.

like image 92
aioobe Avatar answered Jun 08 '26 20:06

aioobe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!