How does the health feature work in Java?
StringBuffer buff1 = new StringBuffer("tuts point");
System.out.println("Old Capacity of buff1 = " + buff1.capacity());
buff1.ensureCapacity(28);
System.out.println("New Capacity of buff1 = " + buff1.capacity());
StringBuffer buff2 = new StringBuffer("compilejksde");
System.out.println("Old Capacity of buff2= " + buff2.capacity());
buff2.ensureCapacity(75);
System.out.println("New Capacity of buff2= " + buff2.capacity());
source to share
The contract states the following :
Ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with more capacity. New potential more:
- The minimumCapacity argument.
- Twice the old container, plus 2.
So, to explain the output from your program:
Old Capacity of buff1 = 26
New Capacity of buff1 = 54 // Old capacity*2+2 (since it larger than the argument: 28)
Old Capacity of buff2= 28
New Capacity of buff2= 75 // the argument you gave, 75 (since it larger than 2*28+2 = 58)
source to share
You probably shouldn't be using StringBuffer
, but StringBuilder
. They both do the same basic functionality, but they StringBuffer
are a thread safe, synchronized class. This additional functionality makes it slower for general single threaded programming.
If you don't have a good reason, use StringBuilder
instead StringBuffer
.
In terms of capacity, StringBuffer
(and StringBuilder
), they have an array char[]
inside that is used to store the characters you add. This array can grow as needed to contain additional characters.
When adding the letter "a" to empty, the StringBuffer
length will be 1 (just one character), but the capacity will be the length of the array char[]
that supports the data, and it should be at least 1 (probably 16). Thus toString()
a
, length()
equal to '1', and the capacity will be about 16.
As data is added, capacity will increase to contain larger rows. Expansion of capacity is relatively slow, especially for large ones StringBuffers
because they require large memory allocations and collections.
If you know how big your data is before you end up in hand, you can increase the capacity to store the final string without having to go through multiple extensions. This saves time.
So if you know the string will be 1000 characters long, then you should ensureCapacity(1000)
sooner and save time later.
source to share
The method java.lang.StringBuffer.ensureCapacity()
guarantees that it is capacity
not less than the equal
specified one minimum
.
If current capacity
- less than the argument
, then "26" is highlighted greater capacity
.
New capacity is larger:
- Argument
minimumCapacity
, -
Twice
old container, plus2
.
See this entire documentation for an example.
public void ensureCapacity(int minimumCapacity)
where minimumCapacity
is the desired capacity minimum
.
source to share
What's happening?
// initial capacity (of the array behind the scenes) will be 16.
StringBuffer buff1 = new StringBuffer("tuts point");
// The above constructor increases the capacity by 16 + stringArgument.length = 16 + 10 = 26 ( hence the output)
System.out.println("Old Capacity of buff1 = " + buff1.capacity()); // 26 ->check
buff1.ensureCapacity(28); // here 28 is minimumcapacity.
// the above line of code calls expandCapacity() of AbstractStringBuilder which does this:
//`(value.length + 1) * 2` i.e, 26+1 * 2 = 54 . Hence the output.
System.out.println("New Capacity of buff1 = " + buff1.capacity()); //54 --> check
PS: The same applies to the second case.
source to share