Java源码学习--001--String

1. 不变性
  • 源码

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];
}
  • 类使用final修饰,不可以被继承
  • 内部维护的字节数组使用final进行修饰,内存地址改变不了,而且地址无法改变
2. 截取
  • 源码
public String substring(int beginIndex, int endIndex) {
     if (beginIndex < 0) {
         throw new StringIndexOutOfBoundsException(beginIndex);
     }
     if (endIndex > value.length) {
         throw new StringIndexOutOfBoundsException(endIndex);
     }
     //获取的字符个数,从beginIndex开始,所以是左包含,右不包含
     int subLen = endIndex - beginIndex;
     if (subLen < 0) {
         throw new StringIndexOutOfBoundsException(subLen);
     }
     return ((beginIndex == 0) && (endIndex == value.length)) ? this
             : new String(value, beginIndex, subLen);
 }

//可以使用字符转成字符串串
public String(char value[], int offset, int count) {
     if (offset < 0) {
         throw new StringIndexOutOfBoundsException(offset);
     }
     if (count < 0) {
         throw new StringIndexOutOfBoundsException(count);
     }
     // Note: offset or count might be near -1>>>1.
     if (offset > value.length - count) {
         throw new StringIndexOutOfBoundsException(offset + count);
     }
     this.value = Arrays.copyOfRange(value, offset, offset+count);
 }


public static char[] copyOfRange(char[] original, int from, int to) {
   int newLength = to - from;
    if (newLength < 0)
        throw new IllegalArgumentException(from + " > " + to);
    char[] copy = new char[newLength];
	//底层真正的方法
    System.arraycopy(original, from, copy, 0,
                     Math.min(original.length - from, newLength));
    return copy;
}
3. 相等判断
  • 源码
public boolean equals(Object anObject) {
	//地址相等就相等
     if (this == anObject) {
         return true;
     }
     if (anObject instanceof String) {
         String anotherString = (String)anObject;
         int n = value.length;
         //依次判断字节数组是否一致
         if (n == anotherString.value.length) {
             char v1[] = value;
             //可以访问对象的私有属性,因为private针对的类,
             //可以在类中使用私有属性,而不是对象
             char v2[] = anotherString.value;
             int i = 0;
             while (n-- != 0) {
                 if (v1[i] != v2[i])
                     return false;
                 i++;
             }
             return true;
         }
     }
     return false;
 }
4. 替换

在这里插入图片描述

  • replace(a,b):将全部的a替换成b
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;
}
  • replaceAll(a,b):批量将全部的a批量更新成b
  • replaceFirst(a,b):将第一个a更新b
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值