【传智播客上海校区】String类的介绍与部分源码分析

String类介绍

String 类是日常开发中使用最频繁的类之一,同时也是非常重要的一个类,因此很有必要针对String类的进一步的理解和分析,而不能仅仅停留在会用的地步。

如下图所示,String类实现了Serializable, Comparable, CharSequence接口。

String类源码

1. 成员变量

String类中包含一个不可变的char数组用来存放字符串,一个int型的变量hash用来存放计算后的哈希值。

//用于存储字符串 private final char value[]; //缓存String的hash值 private int hash; // Default to 0 /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = -6849794470754667710L;

2.构造方法

String有很多构造方法,这边给大家介绍以下几个:

//不含参数的构造函数,一般没什么用,因为value是不可变量 public String() { this.value = new char[0]; } //参数为String类型 public String(String original) { this.value = original.value; this.hash = original.hash; } //参数为char数组,使用java.utils包中的Arrays类复制 public String(char value[]) { this.value = Arrays.copyOf(value, value.length); } //从bytes数组中的offset位置开始,将长度为length的字节,以charsetName格式编码,拷贝到value public String(byte bytes[], int offset, int length, String charsetName) throws UnsupportedEncodingException { if (charsetName == null) throw new NullPointerException(“charsetName”); checkBounds(bytes, offset, length); this.value = StringCoding.decode(charsetName, bytes, offset, length); } //调用public String(byte bytes[], int offset, int length, String charsetName)构造函数 public String(byte bytes[], String charsetName) throws UnsupportedEncodingException { this(bytes, 0, bytes.length, charsetName); }

3. String常用方法

3.1 boolean equals(Object anObject) :将此字符串与指定的对象比较

public boolean equals(Object anObject) { //如果引用的是同一个对象,返回真 if (this == anObject) { return true; } //如果不是String类型的数据,返回假 if (anObject instanceof String) { String anotherString = (String) anObject; int n = value.length; //如果char数组长度不相等,返回假 if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; //从后往前单个字符判断,如果有不相等,返回假 while (n– != 0) { if (v1[i] != v2[i]) return false; i++; } //每个字符都相等,返回真 return true; } } return false; }

equals方法经常用得到,它用来判断两个对象从实际意义上是否相等,String对象判断规则:

1. 内存地址相同,则为真。 2. 如果对象类型不是String类型,则为假。否则继续判断。 3. 如果对象长度不相等,则为假。否则继续判断。 4. 从后往前,判断String类中char数组value的单个字符是否相等,有不相等则为假。如果一直相等直到第一个数,则返回真。

由此可以看出,如果对两个超长的字符串进行比较还是非常费时间的。

3.2 int compareTo(String anotherString):按字典顺序比较两个字符串

public int compareTo(String anotherString) { //自身对象字符串长度len1 int len1 = value.length; //被比较对象字符串长度len2 int len2 = anotherString.value.length; //取两个字符串长度的最小值lim int lim = Math.min(len1, len2); char v1[] = value; char v2[] = anotherString.value; int k = 0; //从value的第一个字符开始到最小长度lim处为止,如果字符不相等,返回自身(对象不相等处字符-被比较对象不相等字符) while (k < lim) { char c1 = v1[k]; char c2 = v2[k]; if (c1 != c2) { return c1 – c2; } k++; } //如果前面都相等,则返回(自身长度-被比较对象长度) return len1 – len2; }

这个方法写的很巧妙,先从0开始判断字符大小。如果两个对象能比较字符的地方比较完了还相等,就直接返回自身长度减被比较对象长度,如果两个字符串长度相等,则返回的是0,巧妙地判断了三种情况。

3.3 boolean startsWith(String prefix,int toffset):测试此字符串是否以指定的前缀开始

public boolean startsWith(String prefix, int toffset) { char ta[] = value; int to = toffset; char pa[] = prefix.value; int po = 0; int pc = prefix.value.length; // Note: toffset might be near -1>>>1. //如果起始地址小于0或者(起始地址+所比较对象长度)大于自身对象长度,返回假 if ((toffset < 0) || (toffset > value.length – pc)) { return false; } //从所比较对象的末尾开始比较 while (–pc >= 0) { if (ta[to++] != pa[po++]) { return false; } } return true; } public boolean startsWith(String prefix) { return startsWith(prefix, 0); } public boolean endsWith(String suffix) { return startsWith(suffix, value.length – suffix.value.length); }

起始比较和末尾比较都是比较经常用得到的方法,例如使用startsWith方法判断一个字符串是不是http协议的,或者使用endsWith方法初步判断一个文件是不是mp3文件。

3.4 String replace(char oldChar,char newChar):它是通过用newChar替换此字符串中出现的所有oldChar返回一个新的字符串

public String replace(char oldChar, char newChar) { //新旧值先对比 if (oldChar != newChar) { int len = value.length; int i = -1; char[] val = value; /* avoid getfield opcode */ //找到旧值最开始出现的位置 while (++i < len) { if (val[i] == oldChar) { break; } } //从那个位置开始,直到末尾,用新值代替出现的旧值 if (i < len) { char buf[] = new char[len]; for (int j = 0; j < i; j++) { buf[j] = val[j]; } while (i < len) { char c = val[i]; buf[i] = (c == oldChar) ? newChar : c; i++; } return new String(buf, true); } } return this; }

这个方法也有讨巧的地方,例如最开始先找出旧值出现的位置,这样节省了一部分对比的时间。

3.5 String trim():忽略前导空白和尾部空白

public String trim() { int len = value.length; int st = 0; char[] val = value; /* avoid getfield opcode */ //找到字符串前段没有空格的位置 while ((st < len) && (val[st] <= )) { st++; } //找到字符串末尾没有空格的位置 while ((st < len) && (val[len – 1] <= )) { len–; } //如果前后都没有出现空格,返回字符串本身 return ((st > 0) || (len < value.length)) ? substring(st, len) : this; }

这个方法使用while循环遍历字符数组前后出现空格字符的索引并记录下来最后使用前后索引进行截取,从而去掉前后空格字符。

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片