What is the setLength() method?

February 9th, 2008 by uCertify Leave a reply »

The setLength() method of the StringBuffer class sets the length of the buffer within the StringBuffer object. Its general form is as follows:

void setLength(int len)

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:

StringBuffer s=new StringBuffer(”have a nice day”);
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”.

Pass SCJA in first attampt. Download free practice test for CX310-019 SCJA exam.
Like this article? Share it with others
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.
  • Share/Bookmark
Advertisement

Leave a Reply

uCertify.com | Our Company | Articles | Contact Us | News and Press Release | uCertify India | Entries (RSS)
MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD    Cisco Certification: CCNA, CCENT    A+, Network+, Security+ Project+
Oracle Certification: OCP 11g, OCP 10g, OCA 11g, OCA 10g    CIW foundation    EC-212-32,    CISSP    Photoshop ACE CS4    Adobe Flash ACE, PMP, CAPM
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.