setLength() StringBuffer Example

setLength() StringBuffer method sets the number of characters that can exist in the buffer. Must be careful in the usage: if the setLengtgh() is set with less size of number of characters, the extra characters in the buffer will be truncated off.

What Java API says ablout setLength() StringBuffer:

public synchronized void setLength(int newLength): sets the capacity for the new length of characters. If the new length specified in the method is greater than the current length, null characters (‘\u0000’) are appended so that the current length becomes new length (null characters are not printed in output, see example). If the newLength is less than the current length, the current length is truncated to the newLength loosing data. If the argument is negative, StringIndexOutOfBoundsException is thrown. See the example.

Following setLength() StringBuffer example illustrates the method with 4 StringBuffer objects.
public class Demo
 public static void main(String args[])
 {					    // new length is less than current length
  StringBuffer sb1 = new StringBuffer("abcdef");
  System.out.println("\nBefore set length sb1 contains characters: " + sb1);  // abcdef
  System.out.println("sb1 current length: " + sb1.length());  // 6
  System.out.println("sb1 current capacity: " + sb1.capacity());  // 22
  System.out.println("After setting length to 4, sb1 contains characters: " + sb1);  // abcd
  System.out.println("sb1 new length: " + sb1.length());  // 4
  System.out.println("sb1 new capacity: " + sb1.capacity());  // 22
					    // new length is greater than current length
  StringBuffer sb2 = new StringBuffer("uvwxyz");
  System.out.println("\nBefore set length sb2 contains characters: " + sb2);  // uvwxyz
  System.out.println("sb2 current length: " + sb2.length());                  // 6
  System.out.println("sb2 current capacity: " + sb2.capacity());              // 22
  System.out.println("After setting length to 10, sb2 contains characters: " + sb2);  // uvwxyz
  System.out.println("sb2 new length: " + sb2.length());  // 10
  System.out.println("sb2 new capacity: " + sb2.capacity());  // 22
					    // new length set to 0
  StringBuffer sb3 = new StringBuffer("pqr");
  System.out.println("\nsb3 characters before setting new length: " + sb3);   // pqr
  System.out.println("sb3 length before setting new length: " + sb3.length());// 3
  System.out.println("sb3 characters after setting new length: " + sb3);      // nothing printed
  System.out.println("sb3 length after setting to 0: " + sb3.length());       // 0
				             // new length as negative number
  StringBuffer sb4 = new StringBuffer("ghi");
  System.out.print("\nsb4 Negative index set: ");

setLength() StringBuffer Example
Output screen of setLength() StringBuffer Example

Observe, setLength() method does not modify the buffer capacity, but modifies the length. length denotes the number of characters present in the buffer.

Leave a Comment

Your email address will not be published.