Java StringBuffer setLength() Method



The Java StringBuffer setLength() method is used to set/change the length of a StringBuffer object.

On invoking this method by passing an integer value representing the new length, the current object is changed to a new object (StringBuffer) whose length is same as the given argument.

A length is nothing but the count of the characters in the given sequence. We have length() method in java, to find the length of the given sequence.

If the newLength argument is greater than or equal to the current length, sufficient null characters ('\u0000') are appended so that length becomes the newLength argument.

The setLength() method accepts a parameter as an integer that holds the value of the newLength. It throws an exception if the given newLength value is negative.

Syntax

Following is the syntax of the Java StringBuffer setLength() method −

public void setLength(int newLength)

Parameters

  • newLength − This is the new length.

Return Value

This method does not return any value.

Example

If the given newLength argument value is positive, the setLength() method sets the new length of the given sequence.

In the following program, we are instantiating the StringBuffer class with the value of “TutorialsPoint”. Using the setLength() method, we are trying to set the new length of the given sequence as 20.

public class SetLength {
    public static void main(String[] args) {
        //instantiating the StringBuffer class
        StringBuffer sb = new StringBuffer("TutorialsPoint");
        System.out.println("StringBuffer: " + sb);
        System.out.println("Before setting the new length the original length is: " + sb.length());
        //initialize the newLength argument value
        int newLength = 20;
        System.out.println("The given newLength argument value is: " + newLength);
        //using the setLength() method
        sb.setLength(newLength);
        System.out.println("After setting the new length the string length is: " + sb.length());
    }
}

Output

On executing the above program, it will produce the following result −

StringBuffer: TutorialsPoint
Before setting the new length the original length is: 14
The given newLength argument value is: 20
After setting the new length the string length is: 20

Example

If the given newLength argument value is negative, the setLength() method throws an IndexOutOfBoundsException.

In the following example, we are creating an object of the StringBuffer class with the value “HelloWorld”. Using the setLength() method, we are trying to set the sequence length as -5.

public class SetLength {
    public static void main(String[] args) {
        try {
            //create an object of the StringBuffer class
            StringBuffer sb = new StringBuffer("HelloWorld");
            System.out.println("StringBuffer: " + sb);
            System.out.println("Before setting the original length is: " + sb.length());
            //initialize the newLength argument value
            int newLength = -5;
            System.out.println("The given newLength argument value is: " + newLength);
            //using the setLength() method
            sb.setLength(newLength);
            System.out.println("After setting is the new length of string is: " + sb.length());
        } catch(IndexOutOfBoundsException e) {
            e.printStackTrace();
            System.out.println("Exception: " + e);
        }
    }
}

Output

Following is the output of the above program −

StringBuffer: HelloWorld
Before setting the original length is: 10
The given newLength argument value is: -5
java.lang.StringIndexOutOfBoundsException: String index out of range: -5
   at java.base/java.lang.AbstractStringBuilder.setLength(AbstractStringBuilder.java:312)
   at java.base/java.lang.StringBuffer.setLength(StringBuffer.java:234)
   at SetLength.main(SetLength.java:14)
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -5

Example

If the given string length is zero, this method sets the new length of the given sequence.

In the following program, we are instantiating the StringBuffer class with an empty value. Then, using the setLength() method, we are trying to set the new length of the given empty sequence as 5.

public class SetLength {
    public static void main(String[] args) {
        //instantiating the StringBuffer class
        StringBuffer sb = new StringBuffer();
        System.out.println("StringBuffer: " + sb);
        System.out.println("Before setting the new length the original length is: " + sb.length());
        //initialize the newLength argument value
        int newLength = 5;
        System.out.println("The given newLength argument value is: " + newLength);
        //using the setLength() method
        sb.setLength(newLength);
        System.out.println("After setting the new length the string length is: " + sb.length());
    }
}

Output

The above program, produces the following results −

StringBuffer:
Before setting the new length the original length is: 0
The given newLength argument value is: 5
After setting the new length the string length is: 5
java_lang_stringbuffer.htm
Advertisements