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.
Now the next methods of discussion are the following:
- Java StringBuffer capacity()
- Java StringBuffer ensureCapacity(int minimumCapacity)
Figure 1 Capacity
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 an 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 newly 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());
- str = new StringBuffer("java");
- System.out.println("Buffer is: " + str);
- System.out.println("Current capacity of buffer is: " + str.capacity());
- str = new StringBuffer(" ");
- System.out.println("Buffer is: " + str);
- System.out.println("Current capacity of buffer is: " + str.capacity());
- StringBuffer str1 = new StringBuffer("Last lecture");
- System.out.println("Buffer is: " + str1);
- System.out.println("Current capacity of buffer is: " + str1.capacity());
- str1 = new StringBuffer("Last lecture of the series");
- System.out.println("Buffer is: " + str1);
- System.out.println("Current capacity of buffer is: " + str1.capacity());
- }
- }
OutputFigure 2
Example: void ensureCapacity(int minimumCapacity)
- 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());
- str.ensureCapacity(12);
- System.out.println("Now capacity of buffer is: " + str.capacity());
- str.ensureCapacity(18);
- System.out.println("Now capacity of buffer is: " + str.capacity());
- StringBuffer str1 = new StringBuffer("Last lecture");
- System.out.println("Buffer is: " + str1);
- System.out.println("Current capacity of buffer is: " + str1.capacity());
- str1.ensureCapacity(32);
- System.out.println("Now capacity of buffer is: " + str1.capacity());
- str1 = new StringBuffer("Last lecture of the series");
- System.out.println("Buffer is: " + str1);
- System.out.println("Current capacity of buffer is: " + str1.capacity());
- str1.ensureCapacity(39);
- System.out.println("Now capacity of buffer is: " + str1.capacity());
- }
- }
OutputFigure 3
The following are the next methods for discussion.
Figure 4
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.
- public class ReverseReplaceMethods {
- public static void main(String args[]) {
- StringBuffer str1 = new StringBuffer("Remember me");
- System.out.println("Output for reverse():");
- System.out.println();
- System.out.println("Buffer is: " + str1);
- System.out.println("Reversed buffer is: " + str1.reverse());
- str1 = new StringBuffer("9876543210");
- System.out.println("Buffer is: " + str1);
- System.out.println("Reversed buffer is: " + str1.reverse());
- str1 = new StringBuffer("pop");
- System.out.println("Buffer is: " + str1);
- System.out.println("Reversed buffer is: " + str1.reverse());
- System.out.println();
- System.out.println("Output for replace(int start, int end , String str):");
- System.out.println();
- StringBuffer str2 = new StringBuffer("Welcome to this article");
- System.out.println("Buffer is: " + str2);
- str2.replace(11, 15, "my");
- System.out.println("After replacement: " + str2);
- str2 = new StringBuffer("Welcome to this article");
- str2.replace(3, 7, "l done");
- System.out.println("After replacement: " + str2);
- str2 = new StringBuffer("Welcome to this article");
- str2.replace(0, 1, "T");
- System.out.println("After replacement: " + str2);
- str2 = new StringBuffer("123000789");
- System.out.println("Buffer is: " + str2);
- str2.replace(3, 6, "456");
- System.out.println("After replacement: " + str2);
- }
- }
OutputFigure 5Methods: setLength() & setCharAt()
The
setLength(int newLength) method sets the new length of the given character sequence whose length is specified by the
newLength argument. If the argument is greater than or equal to the current length then sufficient null characters ("\u0000") are appended so that the length becomes newLength argument. It throws I
ndexOutOfBoundsException() if newLength of the argument is negative.
The
setCharAt(int index, char ch) method sets the characters at the specified
index position to the character ch. The given sequence is altered to represent the new sequence that is similar to the original given sequence, except that it contains ch as the character at the index position. It throws
IndexOutOfBoundsException() if the index is negative or greater than or equal to the length of the given sequence.
Example
In this example, both methods are explained.
- public class SetLengthSetCharAtMethods {
- public static void main(String args[]) {
- StringBuffer str1 = new StringBuffer("Remember me");
- System.out.println("Output for setLength(int newLength):");
- System.out.println();
- System.out.println("Buffer is: " + str1);
- System.out.println("Length of buffer is: " + str1.length());
- str1.setLength(8);
- System.out.println("New Buffer is: " + str1);
- System.out.println("New length of buffer is: " + str1.length());
- str1 = new StringBuffer("9876543210");
- System.out.println("Buffer is: " + str1);
- System.out.println("Length of buffer is: " + str1.length());
- str1.setLength(4);
- System.out.println("New Buffer is: " + str1);
- System.out.println("New length of buffer is: " + str1.length());
- System.out.println();
- System.out.println("Output for setCharAt(int index, char ch):");
- System.out.println();
- StringBuffer str2 = new StringBuffer("Things");
- System.out.println("Buffer is: " + str2);
- System.out.println("Character at index 4: " + str2.charAt(4));
- str2.setCharAt(4, 'k');
- System.out.println("After setting character 'k' at index 4: " + str2);
- System.out.println("Now character at index 4: " + str2.charAt(4));
- str2 = new StringBuffer("7n3");
- System.out.println("Buffer is: " + str2);
- System.out.println("Character at index 1: " + str2.charAt(1));
- str2.setCharAt(1, 'g');
- System.out.println("After setting character 'g' at index 1: " + str2);
- System.out.println("Now character at index 1: " + str2.charAt(1));
- }
- }
OutputFigure 6Methods: subSequence() & getChars()
The
subSequence(int start, int end) method returns a new character sequence that is a subsequence of the given character sequence that is done by
start and
end indices. It throws
IndexOutOfBoundsException() if start or end is negative
or end is greater than the sequence length
or the start is greater than the end.
The
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) method copies the characters from the given sequence into the destination dst char array. The first character is to be copied at index scrBegin whereas the last character is to be copied at the index srcEnd-1. So the total numbers of characters to be copied is srcEnd-srcBegin. The characters copied into the subarray of destination dst starts at the index
dstBegin and ends at the index dstBegin+( srcEnd-srcBegin)-1. This method can throw either of the following two exceptions:
- NullPointerException() if destination dst is found null.
- IndexOutOfBoundsException() if the following are found:
- srcBegin is negativ
- dstBegin is negative
- srcBegin argument > srcEnd argument
- srcEnd >this.length()
- dstBegin+srcEnd-srcBegin > dst.length
Example
In this example, both methods are explained.
- public class SubSeqGetCharsMethods {
- public static void main(String args[]) {
- StringBuffer str1 = new StringBuffer("Authorization");
- System.out.println("Output for subSequence(int start, int end):");
- System.out.println();
- System.out.println("Buffer is: " + str1);
- CharSequence cs1;
- cs1 = str1.subSequence(0, 6);
- System.out.println("subSequence is: " + cs1);
- str1 = new StringBuffer("abcd123efgh");
- System.out.println("Buffer is: " + str1);
- CharSequence cs2;
- cs2 = str1.subSequence(4, 7);
- System.out.println("subSequence is: " + cs2);
- System.out.println();
- System.out.println("Output for getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) ):");
- System.out.println();
- StringBuffer str2 = new StringBuffer("Learning code ");
- System.out.println("Buffer is: " + str2);
- char[] cAR = new char[] {
- 'J', 'a', 'v', 'a', ' ', 'l', 'a', 'n', 'g', 's'
- };
- str2.getChars(9, 13, cAR, 5);
- System.out.print("After copying the characters: ");
- System.out.println(cAR);
- }
- }
OutputFigure 7Your patience is appreciated.
Thank you, keep learning and sharing.