Programming in Android, most of the text values are expected in CharSequence
.
Why is that? What is the benefit, and what are the main impacts of using CharSequence
over String
?
What are the main differences, and what issues are expected, while using them, and converting from one to another?
CharSequence is a contract (interface), and String is an implementation of this contract. The documentation for CharSequence is: A CharSequence is a readable sequence of char values.
A CharSequence is a readable sequence of char values. This interface provides uniform, read-only access to many different kinds of char sequences. A char value represents a character in the Basic Multilingual Plane (BMP) or a surrogate. Refer to Unicode Character Representation for details.
String is a sequence of characters in Java. It is an immutable class and one of the most frequently used types in Java. This class implements the CharSequence, Serializable, and Comparable<String> interfaces.
We can convert a char to a string object in java by using the Character. toString() method.
Strings are CharSequences, so you can just use Strings and not worry. Android is merely trying to be helpful by allowing you to also specify other CharSequence objects, like StringBuffers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With