The 'int count' field in the Java AbstracStringBuilder class is set to / using which method?
My understanding is that StringBuffer uses the values ββof the 'and' count 'fields that StringBuffer inherits from AbstractStringBuilder. The StringBuffer () constructor, for example, calls AbstractStringBuilder (int capacity) to create a 16-bit array using super (16). So far so good how to set "value", but how or in which method is "counting" set / initialized / defined?
+3
source to share
1 answer
count
is initialized to 0 because it represents the number of characters contained in StringBuffer
. Adding characters to StringBuffer
increments the counter.
For example, adding one char increases the count by 1:
public AbstractStringBuilder append(char c) {
int newCount = count + 1;
if (newCount > value.length)
expandCapacity(newCount);
value[count++] = c; // count is incremented
return this;
}
+1
source to share