2

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.

King King
  • 61,710
  • 16
  • 105
  • 130
  • http://stackoverflow.com/questions/12907986/how-could-originalvalue-length-size-happen-in-the-string-constructor?rq=1 – Mob Mar 30 '13 at 09:56

2 Answers2

9

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thank you, now I understand. I'm using Netbeans 7.1 and I think it updates everything for me even JDK, so I thought I'm using the latest version of JDK. I wish I could accept all your answers but this is stackoverflow, not codeproject, sorry others who also helped me. – King King Mar 30 '13 at 10:02
3

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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • I'm failing to see why this answer isn't more upvoted. Doing my part, anyway... :-) Jon's is *also* good, but that doesn't detract from this one. – T.J. Crowder Mar 30 '13 at 09:16
  • I have also up voted NPE and Jon Skeet, in fact, this answer is enough for me (I'm not very new to programming, Jon Skeet's answer is something for a newbie), However I can accept only 1, I wish this can be changed in future. Thanks!!! – King King Mar 30 '13 at 10:06
  • @KingKing: I'm not sure what you mean by my answer being "something for a newbie" - I don't view it as a newbie-oriented answer, given that you're already looking at the internals of String. – Jon Skeet Mar 30 '13 at 10:41
  • @Jon Skeet: I mean your explanation is so clear that a newbie can understand, but I don't think I'm a newbie =)) – King King Mar 30 '13 at 15:40
  • @KingKing: Ah, okay - that makes more sense. – Jon Skeet Mar 30 '13 at 16:40