K-RAN
K-RAN

Reputation: 893

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

Upvotes: 3

Views: 1418

Answers (1)

aioobe
aioobe

Reputation: 420951

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.

Upvotes: 7

Related Questions