The setLength() method of the StringBuffer class sets the length of the buffer within the StringBuffer object. Its general form is as follows:
Here, len specifies the length of the new buffer. Its value should be always non-negative. If the size is set to a value greater than the length of the existing buffer, sufficient null characters will be added to the existing buffer, or if the size is set to a value, which is less than the length of the existing buffer, all characters (including spaces) stored beyond the specified length, will be lost.
For example:
s.setLength(5);
Here, the size of the existing buffer is 15, but it is set to 5. Therefore, all characters (including spaces), which are beyond this limit, will be lost. The new string will be “have”.
Download free practice test for SUN SCJA exam. Click here to get free CX310-019 SCJA exam practice questions.