Help!

A question in book <The Java Tutorial>:

Question 1: What is the initial capacity of the following string buffer?

StringBuffer sb = new StringBuffer("Able was I ere I saw Elba.");

Answer 1: It's the length of the initial string + 16: 26 + 16 = 42.

Why the result is plus 16?

[320 byte] By [JavaEager] at [2007-9-30 14:15:31]
# 1

Good question. I have no idea why they choose to add 16 to the size of the string seems kinda random but I bet there is some good reason for it. Anyway, it is documented as such in the J2SE Javadoc. Here is the relevant quote:

public StringBuffer(String str)

Constructs a string buffer so that it represents the same sequence of characters as the string argument; in other words, the initial contents of the string buffer is a copy of the argument string. The initial capacity of the string buffer is 16 plus the length of the string argument.

Anyone know how they got 16?

caseylou at 2007-7-5 1:29:25 > top of Java-index,Administration Tools,Sun Connection...