0

This code from effective java in Item 51: Beware the performance of string concatenation

    public String statement() {
StringBuilder b = new StringBuilder(numItems() * LINE_WIDTH);
for (int i = 0; i < numItems(); i++)
b.append(lineForItem(i));
return b.toString();
}

can anyone explain to me what is LINE_WIDTH ? what its value ? in this case

Thank so much

das kinder
  • 1,180
  • 2
  • 10
  • 16

1 Answers1

1

I don't know the referenced code, however, from reading the code I would assume:

  • numItems() returns the number of items which are going to be placed into the string, and
  • LINE_WIDTH, is the approximate length (or possibly exact) of a line which will be appended to the string for each item.

The purpose of the code is to reserve enough space in advance of building the string, to prevent new space having to be reallocated during the process of building the string, thus saving in time during the process of building the string.

Mike Curry
  • 1,609
  • 1
  • 9
  • 12