是一个描述字符串结构的接口,有(并非只有)三种常用的子类:
- String
- StringBuffer
- StringBuilder
String |
StringBuffer |
StringBuilder |
public final class String extends Object |
public final class StringBuffer
extends Object |
public final class StringBuilder extends Object |
现在只要有字符串就可以对CharSequence进行实例化:
CharSequence cs = "www.baicu.com"; // 子类型对象向父接口转型 System.out.println(cs );
CharSequence本身是一个接口,在该接口之中也定义有如下操作方法:
- 获取指定索引字符:public char charAt(int index);
- 获取字符串长度:public int length();
- 截取部分字符串: public CharSequence subSequence(int start, int end); CharSequence sub = cs.subSequence(4,8); System.out.println(sub);
只要见到了CharSequence描述的就是字符串。