JDK 源码:String

JDK 源码:String 类详解

String 类是 Java 中最常用的类之一,它表示字符串,是由字符组成的不可变序列。String 类位于 java.lang 包中,Java 的字符串在创建后无法修改,因此它是不可变的(immutable)。下面我们来详细分析 String 类的源码。

1. 基本属性
public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    
    // 字符数组,用于存储字符串的字符
    private final char value[];

    // 字符串的哈希值,用于缓存提高哈希表的性能
    private int hash; // Default to 0
}
  • value:字符数组,存储字符串内容。
  • hash:字符串的哈希值,默认初始化为 0,延迟计算。
2. 构造方法

String 类提供了多种构造方法,允许从不同的数据类型创建字符串:

// 从字符数组构造字符串
public String(char value[]) {
    this.value = Arrays.copyOf(value, value.length);
}

// 从字符数组的子数组构造字符串
public String(char value[], int offset, int count) {
    if (offset < 0) {
        throw new StringIndexOutOfBoundsException(offset);
    }
    if (count < 0) {
        throw new StringIndexOutOfBoundsException(count);
    }
    if (offset > value.length - count) {
        throw new StringIndexOutOfBoundsException(offset + count);
    }
    this.value = Arrays.copyOfRange(value, offset, offset + count);
}

// 从字符串池中获取字符串
public String(String original) {
    this.value = original.value;
    this.hash = original.hash;
}
3. 常用方法

String 类提供了许多常用方法,例如 lengthcharAtsubstringequalshashCode 等。

  • length():返回字符串的长度。
public int length() {
    return value.length;
}
  • charAt(int index):返回指定索引处的字符。
public char charAt(int index) {
    if ((index < 0) || (index >= value.length)) {
        throw new StringIndexOutOfBoundsException(index);
    }
    return value[index];
}
  • substring(int beginIndex, int endIndex):返回一个新的字符串,它是此字符串的一个子字符串。
public String substring(int beginIndex, int endIndex) {
    if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    if (endIndex > value.length) {
        throw new StringIndexOutOfBoundsException(endIndex);
    }
    if (beginIndex > endIndex) {
        throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
    }
    return ((beginIndex == 0) && (endIndex == value.length)) ? this
            : new String(value, beginIndex, endIndex - beginIndex);
}
  • equals(Object anObject):比较字符串的内容是否相同。
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;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}
  • hashCode():返回字符串的哈希码。
public int hashCode() {
    int h = hash;
    if (h == 0 && value.length > 0) {
        char val[] = value;

        for (int i = 0; i < value.length; i++) {
            h = 31 * h + val[i];
        }
        hash = h;
    }
    return h;
}
4. 不可变性

String 类是不可变的,这意味着一旦创建了 String 对象,它的值就不能被更改。其主要原因包括:

  • 线程安全:在多线程环境中,不可变对象天然线程安全,无需同步。
  • 缓存:不可变对象的哈希值可以缓存,提高了性能。
  • 安全性:不可变对象防止被篡改,确保了数据的安全性。

不可变性的实现通过将 value 数组声明为 final,并且不提供任何改变 value 的方法。

总结

String 类是 Java 中的基础类,其设计使其成为不可变对象,提供了丰富的方法来操作字符串。理解 String 的源码可以帮助我们更好地使用字符串,提高代码的性能和安全性。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值