Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choosing between CharSequence and String for an API [duplicate]

Tags:

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(" -->");
   }
}
like image 268
Raedwald Avatar asked Dec 09 '11 12:12

Raedwald


People also ask

What is the difference between CharSequence and String?

A CharSequence is an Interface. String is an immutable sequence of characters and implements the CharSequence interface. CharSequence[] and String[] are just arrays of CharSequence and String respectively.

Does String implement CharSequence?

String is a sequence of characters in Java. It is an immutable class and one of the most frequently used types in Java. This class implements the CharSequence, Serializable, and Comparable<String> interfaces.

How do you turn a String into a char sequence?

We can convert String to char in java using charAt() method of String class. The charAt() method returns a single character only. To get all characters, you can use loop.

What is the use of CharSequence in Java?

A CharSequence is a readable sequence of char values. This interface provides uniform, read-only access to many different kinds of char sequences. A char value represents a character in the Basic Multilingual Plane (BMP) or a surrogate. Refer to Unicode Character Representation for details.


1 Answers

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.

like image 154
Dhananjay Avatar answered Sep 26 '22 03:09

Dhananjay