With this article, the series ends after teaching a few more methods used in the Java StringBuffer class by illustrating simple code examples.
Before reading further, read the following previous parts of the series.
- Learn StringBuffer Class in Java: Lecture 1
- Learn StringBuffer Class in Java: Lecture 2
- Learn StringBuffer Class in Java: Lecture 3
- Learn StringBuffer Class in Java: Lecture 4
- Learn StringBuffer Class in Java: Lecture 5
- Learn StringBuffer Class in Java: Lecture 6
Now the next methods of discussion are the following:
- Java StringBuffer capacity()
- Java StringBuffer ensureCapacity(int minimumCapacity)

The int capacity() method returns the current capacity of the buffer. Here, the capacity is an amount of storage available for the newly inserted characters, beyond which allocation will occur.
The default capacity of the buffer is 16 characters. If the number of characters increases in the buffer from its current capacity, then it increases the capacity by (defaultCapacity+2) if the current capacity is equal to Default capacity or (oldCapacity+2) if the current capacity is different than the default capacity. For example, if the current capacity is 20 and the number of characters is increased by 2 then it will be (20+2)=42.
The void ensureCapacity(int minimumCapacity) method ensures that the given capacity is the minimum of the current capacity. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is larger than the minimum capacity and it is (oldCapacity*2)+2. If the minimumCapacity argument is zero (0) or a negative then it makes no changes and simply returns the current capacity.
Have a look at a sample code example to get a clear picture of the methods.
Example: int capacity()
- public class CapacityMethod
- {
- public static void main(String args[])
- {
- StringBuffer str = new StringBuffer();
- System.out.println("Output for int capacity():");
- System.out.println();
- System.out.println("Default capacity of buffer is: " + str.capacity()); //return default value 16
- str = new StringBuffer("java");
- System.out.println("Buffer is: " + str);
- System.out.println("Current capacity of buffer is: " + str.capacity()); //return 16+4=20
- str = new StringBuffer(" ");
- System.out.println("Buffer is: " + str);
- System.out.println("Current capacity of buffer is: " + str.capacity()); //return 16+1=17
- StringBuffer str1 = new StringBuffer("Last lecture");
- System.out.println("Buffer is: " + str1);
- System.out.println("Current capacity of buffer is: " + str1.capacity()); //returns current capacity of buffer i.e. 16+12=28
- str1 = new StringBuffer("Last lecture of the series");
- System.out.println("Buffer is: " + str1);
- System.out.println("Current capacity of buffer is: " + str1.capacity()); //returns current capacity of buffer that i.e. 16+26=42
- }
- }

Figure 2
- public class EnsureCapacityMethod {
- public static void main(String args[]) {
- StringBuffer str = new StringBuffer();
- System.out.println("Output for void ensureCapacity(int minimumCapacity):");
- System.out.println();
- System.out.println("Default capacity of buffer is: " + str.capacity()); //return default value 16
- str.ensureCapacity(12); //no change will occur because given capacity is less than default capacity
- System.out.println("Now capacity of buffer is: " + str.capacity()); //return default value again
- str.ensureCapacity(18); //now change will occur because given capacity is greater than default capacity
- System.out.println("Now capacity of buffer is: " + str.capacity()); //return (16*2)+2=34
- StringBuffer str1 = new StringBuffer("Last lecture");
- System.out.println("Buffer is: " + str1);
- System.out.println("Current capacity of buffer is: " + str1.capacity()); //returns current capacity of buffer i.e. 16+12=28
- str1.ensureCapacity(32); //increase the current capacity as ensured capacity (32) is greater than the current capacity (28)
- System.out.println("Now capacity of buffer is: " + str1.capacity()); //return (28*2)+2=58
- str1 = new StringBuffer("Last lecture of the series");
- System.out.println("Buffer is: " + str1);
- System.out.println("Current capacity of buffer is: " + str1.capacity()); //returns current capacity of buffer that i.e. 16+26=42
- str1.ensureCapacity(39); //returns current capacity (42) as ensured capacity (39) is less than the current capacity (42)
- System.out.println("Now capacity of buffer is: " + str1.capacity()); //return 42
- }
- }
Output

Figure 3

Methods: reverse() & replace()
The reverse() method returns the string by replacing the given string by its reverse format, or we can say that the string is reversed by this method.
The replace(int start, int end, String str) method is used to replace the characters of the substring or substring of the given string with the specified characters or substrings.
The characters of substrings begin at the startIndex and extend to the characters at endIndex-1 or to the end of the given sequence if no such character exists. The characters in the substring are removed first and then specified string or characters are inserted at startIndex. It throws StringIndexOutOfBoundsException() if startIndex is negative or greater than the length of the given sequence or greater than endIndex.
Example
In this example, both methods are explained.




Santhakumar MunuswamyPosted May 23, 2015, 3:21 PM
Thanks for sharing