Java - Public String(char[] value)

前端 未结 4 1861
夕颜
夕颜 2021-01-20 02:58

My question is about: Public String(char[] value). Can anybody help me: does it internally loops for every value[i] or not. Specifically,

does Public String(char[] v

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-20 03:23

    Java is open source and if you attach the source to Eclipse you can always use F3 to go check the functions. In this case the String class has the following constructor which is what you are looking for:

    /**
     * Allocates a new {@code String} so that it represents the sequence of
     * characters currently contained in the character array argument. The
     * contents of the character array are copied; subsequent modification of
     * the character array does not affect the newly created string.
     *
     * @param  value
     *         The initial value of the string
     */
    public String(char value[]) {
        int size = value.length;
        this.offset = 0;
        this.count = size;
        this.value = Arrays.copyOf(value, size);
    }
    

    Edit: If you are wondering, Arrays.copyOf calls System.arraycopy.

提交回复
热议问题