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