King King
King King

Reputation: 63387

Please explain to me this snippet of code for String constructor in Java?

Here is one of the constructor for String object in Java:

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;
}

The line of code if (originalValue.length > size) is what I care about, I don't think this condition can be true for all the code inside IF being executed. The String is in fact an array of characters. original.count should be equal to its value's length (its value is an array of characters), so the condition wouldn't happen.

I may be wrong, so I need your explanation. Thanks for your help.

VipHaLong.

Upvotes: 2

Views: 224

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1504122

The String is infact an array of characters

No it's not. It's an object which internally has a reference to an array of characters.

original.count should be equal to its value's length (its value is an array of characters)

Not necessarily. It depends on the exact version of Java you're looking at, but until recently several strings could refer to the same char[], each using a different portion of the array.

For example, if you have:

String longString = "this is a long string";
String shortString = longString.substring(0, 2);

... the object referred to shortString would use the same char[] that the original string referred to, but with an start offset of 0 and a count of 2. So if you then called:

String copyOfShortString = new String(shortString);

that would indeed go into the if block you were concerned about in your question.

As of Java 7 update 5, the Oracle JRE has changed to make substring always take a copy. (The pros and cons behind this can get quite complicated, but it's worth being aware of both systems.)

It looks like the version of code you're looking at is an older version where string objects could share an underlying array but view different portions.

Upvotes: 9

NPE
NPE

Reputation: 500943

The String implementation that you are looking at does not copy character data when you create a substring. Instead, multiple String objects can refer to the same character array but have different offset and count (and therefore length).

Therefore, the if condition can, in fact, be true.

Note that this sharing of character arrays has been removed in recent versions of the Oracle JDK.

Upvotes: 3

Related Questions