Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java字符串构造函数逻辑_Java_String - Fatal编程技术网

java字符串构造函数逻辑

java字符串构造函数逻辑,java,string,Java,String,我试图理解java字符串是如何实现的。下面的jdk7源代码显示了对originalValue.length>大小的检查。我不知道它是如何实现的/何时实现的。我试着在一些java字符串创建语句上使用eclipse调试器,但是这个检查从来都不是真的。有可能设计一个字符串参数使这个检查成为真的吗 public final class String{ /** The value is used for character storage. */ private final char va

我试图理解
java字符串
是如何实现的。下面的
jdk7源代码
显示了对
originalValue.length>大小的检查。我不知道它是如何实现的/何时实现的。我试着在一些java字符串创建语句上使用eclipse调试器,但是这个检查从来都不是真的。有可能设计一个字符串参数使这个检查成为真的吗

public final class String{
    /** The value is used for character storage. */
    private final char value[];

    /** The offset is the first index of the storage that is used. */
    private final int offset;

    /** The count is the number of characters in the String. */
    private final int count;

    /** Cache the hash code for the string */
    private int hash; // Default to 0

     /**
     * Initializes a newly created {@code String} object so that it represents
     * the same sequence of characters as the argument; in other words, the
     * newly created string is a copy of the argument string. Unless an
     * explicit copy of {@code original} is needed, use of this constructor is
     * unnecessary since Strings are immutable.
     *
     * @param  original
     *         A {@code String}
     */
    public String(String original) {
        int size = original.count;
        char[] originalValue = original.value;
        char[] v;
        if (originalValue.length > size) {
            // The array representing the String is bigger than the new
            // String itself.  Perhaps this constructor is being called
            // in order to trim the baggage, so make a copy of the array.
            int off = original.offset;
            v = Arrays.copyOfRange(originalValue, off, off+size);
        } else {
            // The array representing the String is the same
            // size as the String, so no point in making a copy.
            v = originalValue;
        }
        this.offset = 0;
        this.count = size;
        this.value = v;
    }
...
}

看看这段代码:

String s1 = "123456789";
String s2 = new String(s1.substring(0, 2));

第二个构造函数将匹配条件。诀窍在于子串方法。它不生成真正的子字符串,而是复制底层数组并为其设置新的边界。构造一个新字符串的想法是复制一个字符串,而不仅仅是分配相同的数组。这就是为什么从大字符串中提取小的子字符串可能导致OOM异常的原因。因为要表示一小段信息,需要使用大数组。

您可以对此进行调试<代码>值
表示底层的
char[]
<代码>计数表示
视图

 String s = new String("Hello   "); //value= [H, e, l, l, o, , , ]  count=8

 String os = s.trim();  //value= [H, e, l, l, o, , , ]  count=5

这里的s2.length>3吗?是的,因为它下面是相同的字符数组(不仅仅是相等的字符数组)。在这种情况下,s2的下划线数组的大小将是2,而不是9。