views:

196

answers:

1

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

+7  A: 

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.

aioobe
Also, String is final (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html), so it cannot be extended.
Toni Menzel