practical learning

String class:

The difference and usage scenarios of StringBuffer and StringBuilder:

 StringBuffer will change the object itself when it is modified. 
       Each result will operate on the StringBuffer object itself, instead of generating a new object and changing the object reference. So in general we recommend using StringBuffer, especially if the string object changes frequently. The main operations on StringBuffer are the append and insert methods.

StringBuffer strBuffer = new StringBuffer("abc");//地址strBuffer,值是abc
strBuffer.append("def");//地址strBuffer,值是abcdef

StringBuilder is mutable object

         This class provides an API compatible with StringBuffer, but synchronization is not guaranteed. This class is designed to be used as a drop-in replacement for StringBuffer when the string buffer is used by a single thread (which is common).

Summarize:

       1. Operate a large amount of data StringBuffer under the multi-threaded operation string buffer; 

       2. Operate a large amount of data StringBuilder under the single-threaded operation string buffer.

3. There is basically no applicable scenario for stringbuffer. In 99% of cases, choose to use stringbuiler in all cases.

Example usage scenarios of "+" and concat methods in String class:

 

System.out.println("***********Transcript************");
     String string="SQL:"+80;
     String string1="JAVA:"+90;
     String string2="HTML:"+86.7;
     System.out.println(string+"  "+string1+"  "+string2);
     String string3="Copyright:";
     String string4="Beida Jade Bird";
     String string5=string3.concat(string4);
     System.out.println();
     System.out.println("             "+string5);

The output is:

          ************Transcript************
SQL:80 JAVA:90 HTML:86.7
                                               Copyright: Beida Jade Bird


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324774574&siteId=291194637