Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

field 'int count' in Java AbstracStringBuilder class is set in/by which method?

I understand StringBuffer uses the fields 'value' and 'count' which StringBuffer inherits from AbstractStringBuilder. The constructor StringBuffer() for example calls on AbstractStringBuilder(int capacity) to create a 16-Bit-array using super(16). So far so good for how to set 'value' but how or in which method is 'count' set/initialized/determined?

like image 416
NeedMoreInfo Avatar asked Jul 21 '26 22:07

NeedMoreInfo


1 Answers

count is initialized to 0, since it represents the number of characters contained in the StringBuffer. Appending characters to the StringBuffer increments the count.

For example, appending a single char increments 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;
}
like image 94
Eran Avatar answered Jul 23 '26 12:07

Eran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!