String源码解析

/**
 * String类代表字符串。 Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例。
 * 字符串不变; 它们的值在创建后不能被更改。 字符串缓冲区支持可变字符串。 因为String对象是不可变的,它们可以被共享
 * code string表示UTF-16格式的字符串
 */
//不可继承
public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** 使用字符数组储存,final不可改变 */
    private final char value[];

    /** 缓存哈希,因为字符串经常用来比较,所以为了性能就直接存储 */
    private int hash; // Default to 0

    /** 序列化值 */
    private static final long serialVersionUID = -6849794470754667710L;

    /**
     * Class String is special cased within the Serialization Stream Protocol.
     *
     * A String instance is written into an ObjectOutputStream according to
     * <a href="{@docRoot}/../platform/serialization/spec/output.html">
     * Object Serialization Specification, Section 6.2, "Stream Elements"</a>
     */
    private static final ObjectStreamField[] serialPersistentFields =
        new ObjectStreamField[0];

1)String类是final类,也即意味着String类不能被继承,并且它的成员方法都默认为final方法。在Java中,被final修饰的类是不允许被继承的,并且该类中的成员方法都默认为final方法。。

    /**
     * 初始一个与参数相同的字符序列,因为字符串不可变,因此除非需要一个副本,否则作用不大
     *
     * @param  original
     *         A {@code String}
     */
    public String(String original) {
        this.value = original.value;
        this.hash = original.hash;
    }

    /**
     * 利用一个已有的字符数组来初始化一个字符串,这是直接重新产生一个新数组,而不仅仅是复制引用,这样以后修改value中的值之后不会影响字符串中的值
     * @param  value
     *         字符串的初始值
     */
    public String(char value[]) {
        this.value = Arrays.copyOf(value, value.length);
    }

用其他String作为参数的时候,虽然直接是数组引用另外一个数组,但String是不可变类型,且对value的操作也都是重新创建一个数组和字符串进行,不会影响原来的

/**
     这是一个有参构造函数,参数为char字符数组,offset(起始位置,偏移量),count(个数)
     * 作用就是在char数组的基础上,从offset位置开始计数count个,构成一个新的String的字符串
     * 意义就类似于截取count个长度的字符集合构成一个新的String对象
     *
     * @param  value
     *         源字符数组
     *
     * @param  offset
     *        初始偏移量
     *
     * @param  count
     *         长度
     *
     * @throws  IndexOutOfBoundsException
     *          If the {@code offset} and {@code count} arguments index
     *          characters outside the bounds of the {@code value} array
     */
    public String(char value[], int offset, int count) {
        if (offset < 0) {
            throw new StringIndexOutOfBoundsException(offset);
        }
        if (count <= 0) {
            if (count < 0) {
                throw new StringIndexOutOfBoundsException(count);
            }
            if (offset <= value.length) {
                this.value = "".value;
                return;
            }
        }
        // Note: offset or count might be near -1>>>1.
        /**
         * 很精髓:这里有可能的问题就是offset+count超出了范围就变为负数那么导致也符合,因此通过offset>value.length-count判断
         * 还有一种判断可以如果offset>0,count>0但offset+count<0来判断||offset+count>value.length来判断
         * */
        if (offset > value.length - count) {
            throw new StringIndexOutOfBoundsException(offset + count);
        }
        this.value = Arrays.copyOfRange(value, offset, offset+count);
    }

可以看出String有对数超范围的处理

  public String(StringBuffer buffer) {
        synchronized(buffer) {
            this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
        }
    }
  public String(StringBuilder builder) {
        this.value = Arrays.copyOf(builder.getValue(), builder.length());
    }

    

根据StringBuffer进行了并发控制,对于StringBuilder则没法做到并发控制

其余部分看:https://blog.csdn.net/snailmann/article/details/80882719

                       http://www.cnblogs.com/dolphin0520/p/3778589

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值