Junior
What is CharSequence?
sobes.tech AI
Answer from AI
CharSequence is an interface in Java that represents a sequence of characters accessible for reading.
Key features:
- Immutability: It represents an immutable sequence of characters.
- Index access: Allows access to characters by their index.
- Subtypes: Implemented by classes such as
String,StringBuilder,StringBuffer.
Main methods:
| Method | Description |
|---|---|
length() |
Returns the length of the character sequence. |
charAt(int index) |
Returns the character at the specified index. |
subSequence(int start, int end) |
Returns a subsequence. |
toString() |
Returns a string representation. |
String represents an immutable sequence of characters, while StringBuilder and StringBuffer provide mutable sequences. CharSequence serves as a common type for working with all these implementations.
Example usage:
// Often used in Android in methods that accept text
void setText(CharSequence text) {
// work with the character sequence
}