jdk源码_String(1)

package com.example.demo;

public class Sort {
    //java源码之string
    public static void main(String[] args) {
        {//空字符对象创建字符串
            String s = new String();
            System.out.println(s);
        }

        {
            //字符串对象创建字符串
            String s = new String("a");
            System.out.println(s);
        }
        {
            //字符数组方式声明字符串
            char[] c = {'a', 'b', 'c'};
            String s = new String(c);
            System.out.println(s);
        }
        {
            /**
             *  // Note: offset or count might be near -1>>>1.
             if (offset > value.length - count) {
             throw new StringIndexOutOfBoundsException(offset + count);
             意思是
             -1>>>1 相当于 integer.maxvalue()
             如果offset + count > value.length 可能会溢出
             而 offset > value.length - count 则不会
             >>>代表无符号又移
             整数源反补都一样
             负数源码第一位是符号位
             负数反码 符号位不变,参照源码其他位按位取反
             符数补码 符号位不变 参照源码 按位取反 末位加一

             }
             */
            //截取字符串
            char[] a = {'a', 'd', 'f', 'r', 'd', 'e'};
            String s = new String(a, 2, 4);
            System.out.println(s);
        }

    }
}
原文地址:https://www.cnblogs.com/xiaoshahai/p/12872448.html