Source code analysis of charsequence interface in jdk8
•
Java
/**
- Created by caoxiaohong on 17/11/18 23:18.
*/import java.lang.*;
/**
- 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)@H_403_22@ or a surrogate. Refer to <a
- href="Character.html#unicode">Unicode Character Representation for details.
- CharSequence就是一个可读的字符序列.对于不同类型的字符序列,这一接口都以统一且只读的方式去读取.
- 一个字符值代表了BMP中的一个字符或者一个代理.(BMP是什么?BMP包含了现代大多数语言的字符集)
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 comparing two
- objects that implement CharSequence 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.
- 这一接口,并没有提炼出Object类定义的equals()方法和hashCode()方法的通用规范(但是,像其他的接口,比如Map就有equals()方法和
- hashCode()方法,再比如,虽然List接口没有给出这两个方法,但是抽象类Abstract重新定义了这两个方法).因此,对于两个不仅实现了
- CharSequence接口的的对象(可能还继承了其他的类),进行比较时,通常,其结果也是未定义的(因为对于继承了Object的类来说,根据具体的实现,* 比较时,是可以有两种选择的,要么比较地址,使用两个等号==,要么比较内容,使用equals.但是这个接口并没有定义equals方法,也没有用到通常我们
- 做两个对象比较时,用到的equals方法和hashCode方法之间的关系,也就是你重写equals方法时,必须重写hashCode方法,这一点我之前在Object
- 源码的分析中说过了).每个对象都可以由不同的类来实现,因此,我们无法保证每个类都有能和其他类实例测试等价性的能力.因此,使用任意的
- CharSequence实例作为set集合的元素或者map中的key,这种做法都是不合适的(为什么不合适呢?因为CharSequence实例是没有equals方法和
- hashCode方法的,所以对应的实例的比较就取决于其继承的类或者其他实现的接口,那么两个被比较的类如果因为继承的类或者实现的接口
- (并且对应继承的类或者接口都对equals和hashCode给出了自己的定义)不同,所以一旦被比较,出现什么结果都是不可控的,故不适合.).
- @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
char
s in the sequence.- 返回字符序列的长度.
- 长度是16bit的整数倍.(因为String类采用的是UTF-16编码,一个字符占用2个字节长度)
- @return the number of
char
s in this sequence
*/
int length();/** * Returns the <code>char</code> value at the specified index. An index ranges from zero * to <tt>length() - 1</tt>. The first <code>char</code> value of the sequence is at * index zero,the next at index one,and so on,as for array * indexing. </p> * * 返回指定索引index位置处的字符.索引index的范围是[0,length()-1]. * * <p>If the <code>char</code> value specified by the index is a * <a href="{@docRoot}/java/lang/Character.html#unicode">surrogate</a>,the surrogate * value is returned. * * 如果指定索引位置处的字符值为代表(字符)(surrogate的出现原因:因为UTF-16采用2个字节存储<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>字符,但是有的字符存储只需要<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>字节,比如英文 * 字符,那么下<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>字节也不能继续存储其他的字符,而只能存储<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>代表字符,来占用这<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>字节的位置,接下来的<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>字节处才能继续存储下<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>字符),* 那么返回的也会是这个代表(字符)值. * * @param index the index of the <code>char</code> value to be returned * * @return the specified <code>char</code> value * * @throws <a href="https://www.jb51.cc/tag/indexoutofboundsexception/" target="_blank" class="keywords">indexoutofboundsexception</a> * if the <tt>index</tt> argument is negative or not less than * <tt>length()</tt> */ char charAt(int index); /** * Returns a new <code>CharSequence</code> that is a subsequence of this sequence. * The subsequence starts with the <code>char</code> value at the specified index and * ends with the <code>char</code> value at index <tt>end - 1</tt>. The length * (in <code>char</code>s) of the * returned sequence is <tt>end - start</tt>,so if <tt>start == end</tt> * then an empty sequence is returned. </p> * * 返回<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>新的字符序列,这个序列是原字符序列的子序列. * 子序列的<a href="https://www.jb51.cc/tag/jiequ/" target="_blank" class="keywords">截取</a>开始位置为:原序列中start的位置; * <a href="https://www.jb51.cc/tag/jiequ/" target="_blank" class="keywords">截取</a>结束位置为:原序列中(end-1)的位置. * 因此子字符序列的长度为(end-start) * 所以,如果传入参数start=end,则返回子序列为空序列. * * @param start the start index,inclusive * @param end the end index,exclusive * * @return the specified subsequence * * @throws <a href="https://www.jb51.cc/tag/indexoutofboundsexception/" target="_blank" class="keywords">indexoutofboundsexception</a> * if <tt>start</tt> or <tt>end</tt> are negative,* if <tt>end</tt> is greater than <tt>length()</tt>,* or if <tt>start</tt> is greater than <tt>end</tt> */ 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. </p> * * 返回字符序列的字符串形式,字符串中字符的顺序和字符序列保持一致.字符串的长度和字符序列一致. * * @return a string consisting of exactly this sequence of characters */ public java.lang.String toString();
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码