What is the difference between CharSequence[] and a String[]?


Java Problem Overview

What is the difference between CharSequence[] and String[]?

Java Solutions

Solution 1 - Java

String implements the CharSequence interface. CharSequence is implemented by String, but also CharBuffer, Segment, StringBuffer, StringBuilder.

So a String[] and a CharSequence[] is essentially the same. But CharSequence is the abstraction, and String is the implementation.

By the way, '[]' denotes an array of objects. So String[] is an array of strings. And String itself is an array of characters.

Solution 2 - Java

CharSequence represents an interface to a sequence of characters, with operations common to all classes implementing it. However, in particular, CharSequence does not make any guarantees about whether the sequence is mutable or not. So, you can have an immutable implementing class, like String or mutable ones, like StringBuilder and StringBuffer.

In addition, CharSequence does not refine the general purpose implementations of the equals() or hashCode() methods, so there is no guarantee that objects of different classes implementing CharSequence will compare to be equal even if the underlying sequence that they hold is the same. So, given:

String seq1 = "hello";
StringBuilder seq2 = new StringBuilder("hello");
StringBuffer seq3 = new StringBuffer("hello");

comparisons between these three using .equals() return false on Java 1.6, but I can't find any guarantees that this will not change in the future (though it's probably fairly unlikely).

And CharSequence[] and String[] are just arrays of their respective types.

EDIT: The practical upshot of this is to compare CharSequences for equality, you have to use their toString() method and compare the resultant Strings, since this is guaranteed to return true if the underlying sequences are the same.

Solution 3 - Java

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.

This means wherever you see CharSequence, you can pass a String object.

Solution 4 - Java

Here is my diagram showing how String is a one of several concrete classes implementing the CharSequence interface.

In particular, StringBuilder is commonly used as a faster alternative to concatenation String objects with the + operator, where thread-safety is not needed.


The square brackets means an array. So, String[] is an array of String objects, and CharSequence[] is an array of objects that may or may not be String objects but definitely are objects implementing the CharSequence interface.

See my Answer here and my Answer here for much more discussion.

Solution 5 - Java

CharSequence is an interface. String is a class that implements the CharSequence interface. That means a String object passes as a CharSequence object. There are different classes that implement the CharSequence interface. They all define the methods that correlate with the method signatures (or abstract methods?) that the interface CharSequence provides.

Solution 6 - Java

String is the parent class that implements the interface CharSequence.

There are other classes which also implement the CharSequence like StringBuffer, StringBuilder, etc.

Solution 7 - Java

CharSequence represents an ordered set of characters, and defines methods for examining this character set. It is ineterface, and one implementation of this interface is String class.

Please refer to Java API documentation for further info. Also, this tutorial might help you: http://download.oracle.com/javase/tutorial/


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrigadierView Question on Stackoverflow
Solution 1 - JavaThierry-Dimitri RoyView Answer on Stackoverflow
Solution 2 - JavaChinmay KanchiView Answer on Stackoverflow
Solution 3 - JavacodinguserView Answer on Stackoverflow
Solution 4 - JavaBasil BourqueView Answer on Stackoverflow
Solution 5 - Javauser28775View Answer on Stackoverflow
Solution 6 - JavaPRaoView Answer on Stackoverflow
Solution 7 - JavaIgorView Answer on Stackoverflow