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”.
Click here to download scja test study guide and practice question. Download free practice test for CX310-019 SCJA exam.If you like this article, please leave a comment or subscribe this blog via RSS or via e-mail, Bookmark and share through your network. Click the AddThis button below. Thanks.
