Java basic knowledge to enhance the stringbuffer of 39:stringbuffer class

Source: Internet
Author: User

1. Add function of StringBuffer:

Public StringBuffer Append (String str): You can add any type of data to the string buffer and return the string buffer itself.

Public stringbuffer Insert (int offset, string str): Inserts any type of data into the string buffer at the specified location and returns the string buffer itself .

Note: Both methods return the string buffer object itself, so that memory can be saved.

2. Case Demo:

1  Packagecn.itcast_02;2 3 /*4 * Add function of StringBuffer:5 * Public stringbuffer Append (String str): You can add any type of data to the string buffer and return the string buffer itself6  * 7 * Public stringbuffer Insert (int offset,string str): Inserts any type of data into the string buffer at the specified location and returns the string buffer itself8  */9  Public classStringbufferdemo {Ten      Public Static voidMain (string[] args) { One         //creating a String buffer object AStringBuffer SB =NewStringBuffer (); -  -         //Public stringbuffer Append (String str) the         //StringBuffer SB2 = sb.append ("Hello"); -         //System.out.println ("SB:" + SB); -         //System.out.println ("SB2:" + SB2); -         //System.out.println (sb = = SB2);//true here is true because both SB and SB2 are pointing to the same StringBuffer object, returning the string buffer itself  +  -         //Add data step by step +         //sb.append ("Hello"); A         //Sb.append (true); at         //sb.append (n); -         //sb.append (34.56); -  -         //Chained Programming -Sb.append ("Hello"). Append (true). Append. Append (34.56); -System.out.println ("SB:" +SB); in  -         //Public stringbuffer Insert (int offset,string to         //str): Inserts any type of data into the string buffer at the specified location and returns the string buffer itself +Sb.insert (5, "World"); -System.out.println ("SB:" +SB); the     } *}

The results of the operation are as follows:

Java basic knowledge to enhance the stringbuffer of 39:stringbuffer class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.