开发者

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 th开发者_开发问答at 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜