I noticed that the capacity method returns StringBuilder capacity without a logic
way ... sometime its value is equals to the string length other t
Here's the logic:
If you define a new instance of the StringBuilder class without a constructor, like so new StringBuilder(); the default capacity is 16.
A constructor can be either an int or a String.
For a String constructor, the default capacity is calculated like this
int newCapacity = string.length() + 16;
For an int constructor, the capacity is calculated like this
int newCapacity = intSpecified + 16;
If a new String is appended to the StringBuilder and the new length of the String is greater than the current capacity, then the capacity is calculated like this:
int newCapacity = (oldCapacity + 1) * 2;