title: Choosing between CharSequence and String for an API
body: <p><a href="https://stackoverflow.com/questions/1391970/how-to-convert-a-string-to-charsequence-in-java">A <code>String</code> <strong>is-a</strong> <code>CharSequence</code></a>. Many methods in the Java library accept <code>CharSequence</code> so <a href="https://stackoverflow.com/questions/1049228/charsequence-vs-string-in-java">they operate more generally</a>. Some classe have a <code>String</code> method (for example, <code>Writer.write(String)</code>) and also implement <code>Appendable</code> with an equivalent <code>CharSequence</code> method (for example, <code>Writer.append(CharSequence)</code>).</p>
  
  <p>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 <code>String</code> or a <code>CharSequence</code>. 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 <code>String</code> rather than a <code>CharSequence</code>. Is there a down-side to using <code>CharSequence</code>? Is there a performance hit? Or is it just programmer intertia or ignorance that causes use of <code>String</code> rather than <code>CharSequence</code>?</p>
  
  <p>Compare</p>
  
  <pre><code>class XMLWriter {
     private final Writer writer;
  
     // more stuff here
  
     public void writeComment(String text) {
        writer.write("&lt;!-- ");
        writer.write(text);
        writer.write(" --&gt;");
     }
  }
  </code></pre>
  
  <p>with</p>
  
  <pre><code>class XMLWriter {
     private final Writer writer;
  
     // more stuff here
  
     public void writeComment(CharSequence text) {
        writer.write("&lt;!-- ");
        writer.append(text);
        writer.write(" --&gt;");
     }
  }
  </code></pre>