3

In String class of java, there is a variable defined as below:

private final int offset;

What does this offset hold?

2
  • The answer is in the very source file containing this variable. Commented Jul 22, 2015 at 13:44
  • Upgrade your Java version. Commented Jul 22, 2015 at 13:53

3 Answers 3

6

From comment of variable offset:

The offset is the first index of the storage that is used.

Internally a String is represented as a sequence of chars in an array.

This is the first char to use from the array.

It has been introducted because some operations like substring create a new String using the original char array using a different offset.

So basically is a variable introduced for a performance tuning for substring operations.

Note: the offset variable is always with the variable private final int count;

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

Comments

3

Form the source code:

The offset is the first index of the storage that is used.

Comments

3

From String.java:

The offset is the first index of the storage that is used.

/** 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;

You can see it being used in various methods such as:

public char charAt(int index) {
    // ...
    return value[index + offset];
}

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.