-1

Why does String class in java have char[] value, int offset and int count fields. What is their purpose and what task do they accomplish?

5
  • 1
    Is S.O. part of your research? Have you done some searching elsewhere and are unable to come up with a viable answer? Commented Mar 12, 2013 at 18:06
  • 1
    I think this is a not unreasonable question Commented Mar 12, 2013 at 18:08
  • @Brian -- I agree, it's just unfit for a coding forum that's designed to help with coding specific problems, not teaching others how to understand coding theory itself. Commented Mar 12, 2013 at 18:11
  • if the string is made from byte[] that would be inhuman. Commented Mar 12, 2013 at 18:18
  • 2
    I think this is a completely reasonable question. Understanding how frameworks implement stuff is a great way to avoid a much more narrow question landing on SO later. Commented Mar 13, 2013 at 0:04

2 Answers 2

3

The char[] array holds the array of characters making up that string.

The offset and count are used for the String.substring() operation. When you take a substring of a string the resultant String references the original character array, but stores an associated offset and length (this is known as a flyweight pattern and is a commonly used technique to save memory)

e.g. String.substring("ABCDEF", 1, 2);

would reference the original array of A,B,C,D,E,F but set offset to 1 and length to 1 (since the substring method uses start and end indices). Note you can do this trivially since the character array is immutable. You can't change it.

Note: This has changed recently (7u6, I believe) and is no longer true in recent versions. I suspect this is due to the realisation that this optimisation isn't really used much.

Sign up to request clarification or add additional context in comments.

Comments

0

They allow passing back and forth an array as a backing for routines that are primarily interested in a portions of the array. This allows one to not worry about constructing tons of small arrays, avoiding the costs associated with object construction for particular operations.

For example, one might use an array as the input buffer, but then need additional arrays to handle the chunked up characters within that buffer, with the triple arguments of array, offset, and count, you can "simulate" reading from the middle of the buffer without the need to create a new (secondary) array.

This is important, as while you might reasonably want an array (an object in java) to hold the input characters, you probably don't want to allocate and garbage collect thousands of arrays (and copy the characters into them) to pass the data into something that only expects a single word, as delimited by white space (hey, it's just an example).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.