String jdk源码查看

String:
String共有15个构造函数 可以有string ,byte,char,stringbuffer,int
String类使用了final修饰符:final类不能被继承,因此final类的成员方法没有机会被覆盖,默认都是final的。在设计类时候,如果这个类不需要有子类,类的实现细节不允许改变,并且确信这个类不会载被扩展,那么就设计为final类 如果一个类不允许其子类覆盖某个方法,则可以把这个方法声明为final方法。
使用final方法的原因有二:
第一、把方法锁定,防止任何继承类修改它的意义和实现。
第二、高效。编译器在遇到调用final方法时候会转入内嵌机制,大大提高执行效率。

实现了Serializable, Comparable<String>, CharSequence接口
Comparable<T>: compareTo()
在string的compareTo实现中:
//anotherString 比较的字符串
public int compareTo(String anotherString) {
int len1 = count; //原对象的长度
int len2 = anotherString.count; //比较对象的长度
int n = Math.min(len1, len2); //判断谁的长度小一点
char v1[] = value; //原对象的值
char v2[] = anotherString.value; //比较对象的值
int i = offset; //原始对象初始值
int j = anotherString.offset; //比较对象的初始值

if (i == j) {
int k = i;
int lim = n + i;
while (k < lim) {
char c1 = v1[k]; //获得原对象char
char c2 = v2[k]; //获得比较对象的char
if (c1 != c2) { //原始对象和比较对象是否相等
return c1 - c2; //不相等直接返回比较的结果
}
k++;
}
} else { //当俩个初始位移量不相等时
while (n-- != 0) {
char c1 = v1[i++];
char c2 = v2[j++];
if (c1 != c2) {
return c1 - c2;
}
}
}
return len1 - len2; //原始对象和比较对象的长度之差 如果为0就表示相等
}
CharSequence:
length() 在string中的实现
public int length() { //返回字符串的长度
return count;
}
charAt()在string中的实现
public char charAt(int index) { //返回改索引的字符
if ((index < 0) || (index >= count)) {
throw new StringIndexOutOfBoundsException(index); //抛出字符串索引超出异常

}
return value[index + offset];
}
subSequence():在string中的实现
public CharSequence subSequence(int beginIndex, int endIndex) {
return this.substring(beginIndex, endIndex);
}



string中的substring方法实现:
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > count) {
throw new StringIndexOutOfBoundsException(endIndex);
}
if (beginIndex > endIndex) {
throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
}
return ((beginIndex == 0) && (endIndex == count)) ? this :
new String(offset + beginIndex, endIndex - beginIndex, value); //从中可以看出 当我们使用的substring方法时还是重新new 对象所有效率不是太高.
}

toString()在string类中的实现
public String toString() { //返回当前的对象
return this;
}

indexOf()在string类中的实现:
static int indexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (targetCount == 0) {
return fromIndex;
}

char first = target[targetOffset];
int max = sourceOffset + (sourceCount - targetCount);

for (int i = sourceOffset + fromIndex; i <= max; i++) {
/* Look for first character. */
if (source[i] != first) {
while (++i <= max && source[i] != first);
}

/* Found first character, now look at the rest of v2 */
if (i <= max) {
int j = i + 1;
int end = j + targetCount - 1;
for (int k = targetOffset + 1; j < end && source[j] ==
target[k]; j++, k++);

if (j == end) {
/* Found whole string. */
return i - sourceOffset;
}
}
}
return -1;
}

String类中的equlas()
public boolean equals(Object anObject) {
if (this == anObject) { //如果是对象 直接判断两个对象引用是否相等
return true;
}
if (anObject instanceof String) { //是字符串
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}

讲讲Object类中的equlas()
public boolean equals(Object obj) { //直接比较的是两个对象的引用是否相等
return (this == obj);
}
Object类中的toString()
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}


---------------
自定义异常:
StringIndexOutOfBoundsException查看源码可知
public class StringIndexOutOfBoundsException extends IndexOutOfBoundsException {

public StringIndexOutOfBoundsException() {
super();
}
public StringIndexOutOfBoundsException(String s) {
super(s);
}
public StringIndexOutOfBoundsException(int index) {
super("String index out of range: " + index);
}
}

假设现在有一个我们自己的异常可以从Exception继承
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值