java charsequence是什么,让我们一起了解一下?
CharSequence类是java.lang包下的一个接口,此接口对多种不同的对char访问的统一接口,像String、StringBuffer、StringBuilder类都是CharSequence的子接口。
那么CharSequence接口是如何实现的?
CharSequence接口的源码如下:
package java.lang; /** * A CharSequence is a readable sequence ofchar
values. This * interface provides uniform, read-only access to many different kinds of *char
sequences. * Achar
value represents a character in the Basic * Multilingual Plane (BMP) or a surrogate. Refer to Unicode Character Representation for details. * *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.
* * @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-bitchar
s in the sequence. * * @return the number ofchar
s in this sequence */ int length(); /** * Returns thechar
value at the specified index. An index ranges from zero * to length() - 1. The firstchar
value of the sequence is at * index zero, the next at index one, and so on, as for array * indexing. * *If the
* * @param start the start index, inclusive * @param end the end index, exclusive * * @return the specified subsequence * * @throws IndexOutOfBoundsException * if start or end are negative, * if end is greater than length(), * or if start is greater than 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(); }char
value specified by the index is a * surrogate, the surrogate * value is returned. * * @param index the index of thechar
value to be returned * * @return the specifiedchar
value * * @throws IndexOutOfBoundsException * if the index argument is negative or not less than * length() */ char charAt(int index); /** * Returns a newCharSequence
that is a subsequence of this sequence. * The subsequence starts with thechar
value at the specified index and * ends with thechar
value at index end - 1. The length * (inchar
s) of the * returned sequence is end - start, so if start == end * then an empty sequence is returned.
另外CharSequence类是一个描述字符串的一个描述型接口,有三个类实现了该接口,分别为:String、StringBuffer、StringBuilder类,所以说,看到这个类,就直接当做是一个字符串类型就可以了,示例代码如下:
/** * String、Stringbuffer、StringBuilder 三个字符相关的类都实现 * 了CharSequence 类,所以说,以后见到CharSequence描述的就是字符串 */ public class CharSequenceStudy { public static void main(String[] args) { CharSequence str = "www.ayit.com"; CharSequence sub = str.subSequence(4,8); System.out.println(sub); System.out.println(str.length()); } }
拓展一下:对于抽象类或者接口来说不可以直接使用new的方式创建对象,但是可以直接给它赋值; CharSequence b = "s"; > 。CharSequence b = "s" 是一个类型强转操作,等于CharSequence b = (CharSequence) new String("s")。
以上就是小编今天的分享了,希望可以帮助到大家。