Section 23rd (String,stringbuffer, 8 wrapper classes corresponding to the underlying type, date-related classes, Random numbers, enum enumerations)

Source: Internet
Author: User
Tags ming

/*java.lang.String is a string type note: Whenever double quotation marks are used to assign a string, the compilation period will be placed in the constant pool of strings in the method area, if the string is run-time. Add or subtract will be placed in the heap (the method is validated before it is placed) Whether the zone contains the same string constant, if present, returns the address, if it does not exist, first puts the string constant in the pool, and then returns the address of the object*/ Public classstringtest01{//the Java portal     Public Static voidMain (string[] args) {//Create an "ABC" string object, the memory address of the object, and let the A1 variable to save//A1 is a reference, a points to an "ABC" ObjectString A1 ="ABC"; //A1 is a local variable, A1 front is not final, so A1 can re-point to//but the "ABCD" string itself is immutableA1 ="ABCD"; System. out. println ("The string is:"+A1); String A2="Ming";//creates a new "Arry" string object in the string constant pool that is immutableString A3 ="Ming";//directly from the string constant pool.System. out. println (A2 = = A3);//true//compares two strings for equality and cannot use "= =",String A4 =NewString ("Ming"); String A5=NewString ("Ming"); System. out. println (a4 = = A5);//                 //To compare the consistency of two strings, you must use the Equals method provided by the string typeSystem. out. println (A4.equals (A5));//trueString S1="a"; String S2="b"; S1= S1 +S2}}
/*The difference between the declaration of a normal string and the creation of a string object 1. String a1 = "Arry"; Only one "Arry" string Object 2 is created in the string constant pool. String a2 = new string ("Arry"); Creates a "Arry" string object in a constant pool of strings and then creates a string object in the heap. The second way is to compare waste memory, the first way of*/ Public classstringtest02{ Public Static voidMain (string[] args) {String A1="Ming"; String A2=NewString ("Ming"); }    }
/*Interview Question: string a1 = new string ("Ming");                String a2 = new String ("Ming");                    How many objects does the above code create? */ Public classstringtest03{ Public Static voidMain (string[] args) {String A1=NewString ("Ming"); String A2=NewString ("Ming"); /*through graphical analysis: 1, created 3 objects, 2 objects in the heap area, 1 recommendations in a constant pool: when using string It is not recommended to use the New keyword because it creates two object summaries: The heap is allocated in the trial run, and the constant pool is allocated at compile time.*/            }}
/*Note When using string: Try not to frequent splicing operations, as the string once created immutable, as long as the frequent splicing, will be in the string constant pool to create a large number of string objects, garbage collection brings problems*/ Public classstringtest04{ Public Static voidMain (string[] args) {string[] s= {"Sport","Music"," Food","Sleep"}; //requirement: To stitch all interests into string "sport,music,food,sleep";String Temp="";  for(inti =0; i < s.length; i++){            if(i = = s.length-1) {Temp+=S[i]; } Else{Temp+ = s[i]+","; }} System. out. println ("stitching a good string:"+temp); }}
/*How to construct strings commonly*/ Public classstringtest05{ Public Static voidMain (string[] args) {String A1="Ming"; String A2=NewString ("Ming"); byte[] bytes = {1,2,3,4}; String A3=NewString (bytes); System. out. println (A3); String A4=NewString (Bytes,1,2); System. out. println (A4); Char[] A5 = {'I'm','is a','A'}; String s=NewString (A5); System. out. println (s); //constructs a new String by using the platform's default character set to decode the specified byte subarray. String A6 =NewString (A5,2,1); System. out. println (A6); }}

Section 23rd (String,stringbuffer, 8 wrapper classes corresponding to the underlying type, date-related classes, Random numbers, enum enumerations)

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.