StringBuffer setLength() Method in Java: Usage and Examples

Rumman Ansari   Software Engineer   2024-07-05 03:39:02   6195  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

To set the length of the buffer within a StringBuffer object, use setLength( ).

Syntax

public void setLength(int newLength)

Parameters

newLength - the new length

Throws

IndexOutOfBoundsException - if the newLength argument is negative.

Discussion

This method 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.

The newLength argument must be greater than or equal to 0.

Program

Following setLength() StringBuffer example illustrates the method with 4 StringBuffer objects.

public class DemosetLength
{
 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
  sb1.setLength(4);
  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
  sb2.setLength(10);
  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
  sb3.setLength(0);
  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: ");
  sb4.setLength(-5);
 }
}

Output


Before set length sb1 contains characters: abcdef
sb1 current length: 6
sb1 current capacity: 22
After setting length to 4, sb1 contains characters: abcd
sb1 new length: 4
sb1 new capacity: 22

Before set length sb2 contains characters: uvwxyz
sb2 current length: 6
sb2 current capacity: 22
After setting length to 10, sb2 contains characters: uvwxyz
sb2 new length: 10
sb2 new capacity: 22

sb3 characters before setting new length: pqr
sb3 length before setting new length: 3
sb3 characters after setting new length:
sb3 length after setting to 0: 0

sb4 Negative index set: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out
 of range: -5
        at java.lang.AbstractStringBuilder.setLength(AbstractStringBuilder.java:207)
        at java.lang.StringBuffer.setLength(StringBuffer.java:192)
        at DemosetLength.main(DemosetLength.java:33)
Press any key to continue . . .