In String class of java, there is a variable defined as below:
private final int offset;
What does this offset hold?
In String class of java, there is a variable defined as below:
private final int offset;
What does this offset hold?
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;
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];
}