20

A String is-a CharSequence. Many methods in the Java library accept CharSequence so they operate more generally. Some classe have a String method (for example, Writer.write(String)) and also implement Appendable with an equivalent CharSequence method (for example, Writer.append(CharSequence)).

If I am writing a class that delegates to such a class, ands needs some text input, I can choose for that input to be a String or a CharSequence. Choosing the later makes the class more flexible, by giving the client more options. But I don't see much code that does so: text arguments are almost invariably a String rather than a CharSequence. Is there a down-side to using CharSequence? Is there a performance hit? Or is it just programmer intertia or ignorance that causes use of String rather than CharSequence?

Compare

class XMLWriter {
   private final Writer writer;

   // more stuff here

   public void writeComment(String text) {
      writer.write("<!-- ");
      writer.write(text);
      writer.write(" -->");
   }
}

with

class XMLWriter {
   private final Writer writer;

   // more stuff here

   public void writeComment(CharSequence text) {
      writer.write("<!-- ");
      writer.append(text);
      writer.write(" -->");
   }
}
Community
  • 1
  • 1
Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • 1
    Your logic makes a lot of sense to me. By using CharSequence you would allow calling code to supply a StringBuffer/StringBuilder without needing to invoke toString() on it -- which happens a lot. – Andy Dec 09 '11 at 12:17
  • @Andy yes, I had `StringBuilder` in mind when I wrote the question. – Raedwald Dec 09 '11 at 12:20
  • See this Question’s later duplicate, [When to use CharSequence in an API](http://stackoverflow.com/q/13234584/642706). – Basil Bourque Jul 01 '15 at 18:44
  • For more discussion, see also the Question, [CharSequence VS String in Java?](http://stackoverflow.com/q/1049228/642706), and its duplicate, [Exact difference between CharSequence and String](http://stackoverflow.com/q/11323962/642706) in java. And [my class diagram](http://i.stack.imgur.com/PIFk9.png). – Basil Bourque Jul 01 '15 at 18:47
  • Closed as a duplicate of the newer question, since that question attracted some thorough answers. – Duncan Jones May 21 '17 at 05:54

1 Answers1

16

Quoting CharSequence Javadoc:

This interface does not refine the general contracts of the equals and hashCode methods. The result of testing two objects that implement CharSequence for equality is therefore, in general, undefined. Each object may be implemented by a different class, and there is no guarantee that each class will be capable of testing its instances for equality with those of the other. It is therefore inappropriate to use arbitrary CharSequence instances as elements in a set or as keys in a map.

Hence IMO We must think twice before using CharSequnce as a replacement for String.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Dhananjay
  • 3,903
  • 2
  • 29
  • 44
  • Would you say that methods that merely output text, such as my example, should use `CharSequence` rather than `String`? – Raedwald Dec 12 '11 at 13:23
  • 1
    Good Answer, thanks for quoting the Javadoc. But I disagree with your conclusion. The concern about using the `CharSequence` returned by some API belongs to the *calling programmer* rather than the author of the API. If the calling programmer goes off to use the resulting text for a purpose such as a key in a map or adding to a set, they should should be the one to think twice. And then they should simply call [`CharSequence::toString`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/CharSequence.html#toString()). The author of an API should not worry about misuse later. – Basil Bourque Nov 12 '19 at 22:42