Java StringBuffer setLength() Method

The StringBuffer.setLength() method in Java is used to set the length of the character sequence in the StringBuffer object. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. setLength Method Syntax
  3. Examples
    • Setting Length to a Smaller Value
    • Setting Length to a Larger Value
  4. Conclusion

Introduction

The setLength() method is a member of the StringBuffer class in Java. It allows you to change the length of the StringBuffer. If the new length is shorter than the current length, the sequence is truncated. If the new length is greater than the current length, the sequence is padded with \0 (null characters).

setLength Method Syntax

The syntax for the setLength method is as follows:

public synchronized void setLength(int newLength)

Parameters:

  • newLength - the new length of the StringBuffer.

Throws:

  • IndexOutOfBoundsException - if the newLength argument is negative.

Examples

Setting Length to a Smaller Value

The setLength method can be used to truncate the content of a StringBuffer object.

Example

public class StringBufferSetLengthExample {
    public static void main(String[] args) {
        // Create a StringBuffer object with initial content
        StringBuffer sb = new StringBuffer("Hello, World!");

        // Set the length to a smaller value
        sb.setLength(5);

        // Print the modified content of the StringBuffer
        System.out.println(sb.toString());
    }
}

Output:

Hello

Setting Length to a Larger Value

The setLength method can also be used to increase the length of a StringBuffer object, padding with null characters.

Example

public class StringBufferSetLengthExample {
    public static void main(String[] args) {
        // Create a StringBuffer object with initial content
        StringBuffer sb = new StringBuffer("Hello");

        // Set the length to a larger value
        sb.setLength(10);

        // Print the modified content of the StringBuffer
        System.out.println("Content: '" + sb.toString() + "'");

        // Print the content character by character
        for (int i = 0; i < sb.length(); i++) {
            System.out.print((int) sb.charAt(i) + " ");
        }
    }
}

Output:

Content: 'Hello     '
72 101 108 108 111 0 0 0 0 0

Handling Edge Cases

Attempting to set a negative length will result in an IndexOutOfBoundsException.

Example

public class StringBufferSetLengthExample {
    public static void main(String[] args) {
        // Create a StringBuffer object with initial content
        StringBuffer sb = new StringBuffer("Hello, World!");

        try {
            // Attempt to set a negative length
            sb.setLength(-1);
        } catch (IndexOutOfBoundsException e) {
            // Handle the exception
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Output:

Error: newLength must be non-negative

Conclusion

The StringBuffer.setLength() method in Java provides a way to change the length of a StringBuffer object. By understanding how to use this method, you can efficiently manage the content of your StringBuffer, whether you need to truncate it or extend it with padding. This method is particularly useful for applications that require dynamic adjustment of string lengths.

Comments