Java basic Knowledge strengthening the construction method of the StringBuffer of 38:stringbuffer class

Source: Internet
Author: User

1. How to construct StringBuffer:

(1) StringBuffer ():

(2) StringBuffer (charsequence seq):

(3) StringBuffer (int capacity):

(4) StringBuffer (String str):

2. Case Demo:

1  Packagecn.itcast_01;2 3 /*4 * Thread Safety (multi-threaded tutorial)5 * Security--sync--Data is safe6 * Unsafe--different steps--higher efficiency7 * Safety and efficiency issues are a problem that will haunt us forever. 8 * Security: Hospital's website, Bank website9 * Efficiency: News websites, forums and the likeTen  *  One * StringBuffer: A * thread-safe mutable strings.  -  *  - * What is the difference between StringBuffer and string? the * The former is variable in length and content and the latter is immutable.  - * If you use the former to do string concatenation, will not waste too much resources.  -  *  - * Construction method of StringBuffer: + * Public StringBuffer (): Non-parametric construction method - * Public stringbuffer (int capacity): Specifies the capacity of the string buffer object + * Public StringBuffer (String str): String buffer object specifying string contents A  * at * Method of StringBuffer: - * public int capacity (): Returns the current capacity. Theoretical value - * public int length (): Returns the length (number of characters). Actual value -  */ -  Public classStringbufferdemo { -      Public Static voidMain (string[] args) { in         //Public StringBuffer (): Non-parametric construction method -StringBuffer SB =NewStringBuffer (); toSystem.out.println ("SB:" +SB); +System.out.println ("Sb.capacity ():" +sb.capacity ()); -System.out.println ("Sb.length ():" +sb.length ()); theSystem.out.println ("--------------------------"); *  $         //Public stringbuffer (int capacity): Specifies the capacity of the string buffer objectPanax NotoginsengStringBuffer SB2 =NewStringBuffer (50); -System.out.println ("SB2:" +SB2); theSystem.out.println ("Sb2.capacity ():" +sb2.capacity ()); +System.out.println ("Sb2.length ():" +sb2.length ()); ASystem.out.println ("--------------------------"); the  +         //Public StringBuffer (String str): String buffer object specifying string contents -StringBuffer SB3 =NewStringBuffer ("Hello"); $System.out.println ("SB3:" +SB3); $System.out.println ("Sb3.capacity ():" +sb3.capacity ()); -System.out.println ("Sb3.length ():" +sb3.length ()); -     } the}

The results of the operation are as follows:

Java basic Knowledge strengthening the construction method of the StringBuffer of 38: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.