CharSequence
is rarely used in general purpose libraries. It should usually be used when your main use case is string handling (manipulation, parsing, ...).
Generally speaking you can do anything with a CharSequence
that you could do with a String
(trivially, since you can convert every CharSequence
into a String
). But there's one important difference: A CharSequence
is not guaranteed to be immutable! Whenever you handle a String
and inspect it at two different points in time, you can be sure that it will have the same value every time.
But for a CharSequence
that's not necessarily true. For example someone could pass a StringBuilder
into your method and modify it while you do something with it, which can break a lot of sane code.
Consider this pseudo-code:
public Object frobnicate(CharSequence something) {
Object o = getFromCache(something);
if (o == null) {
o = computeValue(something);
putIntoCache(o, something);
}
return o;
}
This looks harmless enough and if you'd had used String
here it would mostly work (except maybe that the value might be calculated twice). But if something
is a CharSequence
then its content could change between the getFromCache
call and the computeValue
call. Or worse: between the computeValue
call and the putIntoCache
call!
Therefore: only accept CharSequence
if there are big advantages and you know the drawbacks.
If you accept CharSequence
you should document how your API handles mutable CharSequence
objects. For example: "Modifying an argument while the method executes results in undefined behaviour."
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…