CharSequence初探

Posted 沛沛老爹

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CharSequence初探相关的知识,希望对你有一定的参考价值。

背景

在使用hutool的StrUtil的时候,Sonar提示有问题。建议使用CharSequenceUtil来替换.

好奇心的驱使下,想了解下CharSequence这个到底有什么不一样的地方。

CharSequence

CharSequence类是java.lang包下的一个接口,此接口对多种不同的对char访问的统一接口,像String、StringBuffer、StringBuilder类都是CharSequence的子接口;

CharSequence类和String类都可以定义字符串,但是String定义的字符串只能读,CharSequence定义的字符串是可读可写的;

对于抽象类或者接口来说不可以直接使用new的方式创建对象,但是可以直接给它赋值

源码


/**
 * A @code CharSequence is a readable sequence of @code char values. This
 * interface provides uniform, read-only access to many different kinds of
 * @code char sequences.
 * A @code char value represents a character in the <i>Basic
 * Multilingual Plane (BMP)</i> or a surrogate. Refer to <a
 * href="Character.html#unicode">Unicode Character Representation</a> for details.
 *
 * <p> This interface does not refine the general contracts of the @link
 * java.lang.Object#equals(java.lang.Object) equals and @link
 * java.lang.Object#hashCode() hashCode methods. The result of testing two objects
 * that implement @code 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 @code CharSequence instances as elements in a set or as keys in
 * a map. </p>
 *
 * @author Mike McCloskey
 * @since 1.4
 * @spec JSR-51
 */

public interface CharSequence 

    /**
     * Returns the length of this character sequence.  The length is the number
     * of 16-bit @code chars in the sequence.
     *
     * @return  the number of @code chars in this sequence
     */
    int length();

    /**
     * Returns the @code char value at the specified index.  An index ranges from zero
     * to @code length() - 1.  The first @code char value of the sequence is at
     * index zero, the next at index one, and so on, as for array
     * indexing.
     *
     * <p>If the @code char value specified by the index is a
     * <a href="@docRoot/java.base/java/lang/Character.html#unicode">surrogate</a>, the surrogate
     * value is returned.
     *
     * @param   index   the index of the @code char value to be returned
     *
     * @return  the specified @code char value
     *
     * @throws  IndexOutOfBoundsException
     *          if the @code index argument is negative or not less than
     *          @code length()
     */
    char charAt(int index);

    /**
     * Returns a @code CharSequence that is a subsequence of this sequence.
     * The subsequence starts with the @code char value at the specified index and
     * ends with the @code char value at index @code end - 1.  The length
     * (in @code chars) of the
     * returned sequence is @code end - start, so if @code start == end
     * then an empty sequence is returned.
     *
     * @param   start   the start index, inclusive
     * @param   end     the end index, exclusive
     *
     * @return  the specified subsequence
     *
     * @throws  IndexOutOfBoundsException
     *          if @code start or @code end are negative,
     *          if @code end is greater than @code length(),
     *          or if @code start is greater than @code end
     */
    CharSequence subSequence(int start, int end);

    /**
     * Returns a string containing the characters in this sequence in the same
     * order as this sequence.  The length of the string will be the length of
     * this sequence.
     *
     * @return  a string consisting of exactly this sequence of characters
     */
    public String toString();

    /**
     * Returns a stream of @code int zero-extending the @code char values
     * from this sequence.  Any char which maps to a <a
     * href="@docRoot/java.base/java/lang/Character.html#unicode">surrogate code
     * point</a> is passed through uninterpreted.
     *
     * <p>The stream binds to this sequence when the terminal stream operation
     * commences (specifically, for mutable sequences the spliterator for the
     * stream is <a href="../util/Spliterator.html#binding"><em>late-binding</em></a>).
     * If the sequence is modified during that operation then the result is
     * undefined.
     *
     * @return an IntStream of char values from this sequence
     * @since 1.8
     */
  ...

也就是说,所有的字符类型都需要实现CharSequence这个接口

实现校验

为了验证下真伪,我们找到对应的源码

String

...

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence 

     
...

StringBuffer

 public final class StringBuffer
    extends AbstractStringBuilder
    implements java.io.Serializable, Comparable<StringBuffer>, CharSequence


...

 StringBuilder

public final class StringBuilder
    extends AbstractStringBuilder
    implements java.io.Serializable, Comparable<StringBuilder>, CharSequence

总结

CharSequence 是一个接口,String实现了当前接口CharSequence是一个接口,用于表示有序的字符集合,并提供了一些基本的操作方法。

String StringBuffer StringBuilder都实现了CharSequence这个接口

以上是关于CharSequence初探的主要内容,如果未能解决你的问题,请参考以下文章

初探Django

如何将字符串转换为 CharSequence?

Redis初探

法 setLatestEventInfo(TermService,CharSequence,CharSequence,PendingIntent)

是否可以在Firestore中保存CharSequence?

初探nfs-ganesha