Java Learning Note--string StringBuffer StringBuilder

Source: Internet
Author: User

String StringBuffer StringBuilder

String http://docs.oracle.com/javase/7/docs/api/

English: http://www.cnblogs.com/YSO1983/archive/2009/12/07/1618564.html

String STR0= "Most normal method of creation";                System.out.println (STR0); Char[] C1 =New Char[3]; c1[0] = ' C '; c1[1] = ' B '; c1[2] = ' a '; String str1=NewString ("string1"); String str2=NewString (C1); System.out.println (str1+ "" + str2);//output string1 CBA                /*string.valueof () It can convert Java primitives (Int,double,boolean, etc.) and objects (object) to String * toString () is a method of an object that converts the object to a string type, The conversion algorithm depends on the actual needs of the type, * basically every object in Java has a ToString method. */        //String Conversions        intI1 = 100; String STR3=integer.tostring (I1); System.out.println ("Integer.tostring:" +STR3);//Output integer.tostring:100//valueOf () for data conversionString Str32 = string.valueof (123456); System.out.println ("String.valueof ():" +str32);//output string.valueof (): 123456//Character Extraction//charAtString STR4 = "A long string!"; System.out.println ("CharAt (3):" +str4.charat (3));//Output Charat (3): O//GetChars ()        Charbuf [] =New Char[6-2]; Str4.getchars (2, 6, buf, 0); System.out.print ("GetChars Output:"); System.out.println (BUF); //output GetChars output: Long//char[] ToCharArray ()System.out.println (Str4.tochararray ());//output a long string! //extracting substringsString STR5 = str4.substring (2,8); System.out.println ("SubString (2,8) output:" +STR5);//output substring (2,8) output: Long S    

StringBuffer http://docs.oracle.com/javase/7/docs/api/

        /** StringBuffer () * stringbuffer (int size) * StringBuffer (String str) * Receives a string variable, Specify the initial contents of the StringBuffer object and reserve 16 additional character space * StringBuffer (charsequence chars)*/StringBuffer SBF=NewStringBuffer ("Hello"); System.out.println ("Buffer =" +SBF); System.out.println ("Length =" +sbf.length ()); System.out.println ("Capacity =" +sbf.capacity ()); System.out.println ("----Reassign buffer size----"); Sbf.ensurecapacity (55); System.out.println ("Buffer =" +SBF); System.out.println ("Length =" +sbf.length ()); System.out.println ("Capacity =" +sbf.capacity ()); System.out.println ("----Set buffer length----"); Sbf.setlength (30); System.out.println ("Buffer =" +SBF); System.out.println ("Length =" +sbf.length ()); System.out.println ("Capacity =" +sbf.capacity ()); 

Output Result:

Buffer == 5=----Re-specify the buffer size----= =5 =----Set the buffer length----= Hello

StringBuilder http://docs.oracle.com/javase/7/docs/api/

In addition to an important distinction, stringbuffer equates to StringBuilder, the difference being that it is not synchronous, meaning it is not thread-safe.

The StringBuilder advantage is faster performance, but you must use StringBuffer when using multi-threading, and you cannot use StringBuilder

Java Learning Note--string StringBuffer StringBuilder

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.