Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CharSequence VS String in Java?

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?

like image 686
e-satis Avatar asked Jun 26 '09 13:06

e-satis


People also ask

What is the difference between CharSequence and string in Java?

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.

What is CharSequence in Java?

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.

Does string implement CharSequence?

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.

Can you convert char to string?

We can convert a char to a string object in java by using the Character. toString() method.


1 Answers

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.

like image 54
Zarkonnen Avatar answered Sep 29 '22 12:09

Zarkonnen