Java SE string class [1]

Source: Internet
Author: User

  There are two final classes used to process characters: string class and stringbuffer class.1. The string class implements an immutable string. the immutable string can be read after being created and initialized. 2. The stringbuffer class implements a dynamic character string. 3. The character string implemented using these two classes is a real object. Each character of this string is represented by a 16-bit character. Create and initialize a string1. The simplest way to create a String object is to use a String constant: String str1 = "You cannot change me! ";/* In a sense: A = new A (); a B = new A (); A = B; * // * str1 and "You cannot change me!" Are all objects. */String constants are references to string objects. The value in the string object is the Character Sequence of the String constant in double quotation marks. Since a String constant is a reference, you can operate on it like other string references. 2. The String constant can be used to call the method on its String object: int Len = "You cannot change me! ". Length (); 3. Interned string <1> If the value-bit string of the constant expression and the string sequence of the constant expression are the same, these share the same string object. When the string is interned, the string class protects the same private pool. String str2 = "You cannot change me! "; <2> string reference str1 and str2 both represent the same string object and are initialized as character strings:" You cannot change me! ". <3> when this expression is deduced during compilation, An interned string is generated. <4> In the following code, can1 reference and can2 reference indicate the same string object containing the string "7up. String can1 = 7 + "up"; string can2 = "7up"; <5> however, in the following code, can4 is referenced to indicate a New String object. during compilation, the value is "7up ". String word = "up"; string can4 = 7 + word; constant expressions with a value of string share the same string object, because the string object is unchangeable. The expression (7 + other characters) is not a constant-to-ratio expression, so a new String object is generated. String ConstructorThe string class has many constructors. You can use different types of parameters to create and initialize string objects. The following are two constructors: 1. String (string s) This constructor creates a new String object with the same content as the string object parameter. 2. String () <1> This constructor creates a new String object with a blank string "". <2> note: the constructor is used to create a new String object instead of sharing strings. <3> call the intern () method in the string class to obtain the reference of the inerned string. However, this is not required. 3. Other constructors. You can also use byte arrays, character arrays, and string buffering to create string objects: byte [] bytes = {97,98, 98,97}; char [] characters = {'A ', 'B', 'B', 'A'}; stringbuffer strbuf = new stringbuffer ("Abba ");//...... String bytestr = new string (bytes); // using array of bytes: "Abba" string charstr = new string (character); // using array of chars: "Abba" string buffstr = new string (strbuf); // using string of buffer: "Abba" Instance1. In the following example, the string object referenced by str1 is different from the string object represented by str4 and str5. 2. Use the new operator and string constructor to always create a new String object. 3. The expression ("You cannot" + other characters) is not a constant expression, so a new stiring object is generated. 4. Local references to str2, str3, and static references to str1 in the main () method indicate the same interned string. Code: PackageStringdemo; Public ClassStringconstruction {/*** @ ParamARGs */ StaticString Str1= "You cannot change me! "; // Interned Public Static VoidMain (string [] ARGs ){// TodoAuto-generated method stub string emptystr = NewString (); // "" system. Out. Println ("emptystr" + emptystr); string str2 = "You cannot change me! "; // Interned string str3 =" You cannot "+" change me! "; // Interned string str4 = NewString ("You cannot change me! "); // New String object string words =" change me "; string str5 =" You cannot "+ words; // New String object system. Out. Println ("str1 = str2:" + ( Str1= Str2); // (1) True system. Out. Println ("str1.equals (str2)" + Str1. Equals (str2); // (2) True system. Out. Println ("str1 = str3:" + ( Str1= Str3); // (3) True system. Out. Println ("str1.equals (str3)" + Str1. Equals (str3); // (4) True system. Out. Println ("str1 = str4:" + ( Str1= Str4); // (5) False system. Out. Println ("str1.equals (str4)" + Str1. Equals (str4); // (6) True system. Out. Println ("str1 = str5:" + ( Str1= Str5); // (7) False system. Out. Println ("str1.equals (str5)" + Str1. Equals (str5); // (8) True system. Out. Println ("str1 = aupoliciary. str1:" + ( Str1= Aupoliciary. Str1); // (9) True system. Out. Println ("str1.equals (aupoliciary. str1)" + Str1. Equals (aupoliciary. Str1); // (10) True system. Out. Println ("/" You cannot change me! /". Length ():" + "You cannot change me! ". Length ());}} ClassAuxiliary { StaticString Str1= "You cannot change me! ";}

 

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.