A summary of the problems of 05-string and the experimental problems after class

Source: Internet
Author: User

One. Please run the following instance code Stringpool.java to see its output. How do I interpret this output? What can you sum up from it?

(1) In Java, the same character constants ("Hello") with the same contents save only one copy to conserve memory, so s0,s1,s2 actually refers to the same object.

(2) When compiling a s2 sentence, the compiler will remove the "+" number and directly concatenate the two strings together to get a string ("Hello"). This optimization work is done automatically by the Java compiler.

(3) When creating a string object directly using the New keyword, the values are consistent (all "Hello"), but are still two separate objects.

Look again ...

Why does the above output result? What can you sum up from it?

(1) Assigning a value to a string variable means: Two variables (S1,S2) now refer to the same string Object "a"!

(2) The content of the string object is read-only , and the value of the S1 variable is modified with "+", in effect, a new string Object is obtained with the content "AB", which is independent of the object "A" referenced by the original S1, sos1== S2 returns false;

(3) the "AB" string in the code is a constant that refers to a string that is not related to the "AB" object referenced by S1.

(4) The String.Equals () method can compare the contents of two strings.

Two. Please review the implementation Code of the String.Equals () method and learn how to implement it.

  Public classStringequals {/**     * @paramargs the command line arguments*/         Public Static voidMain (string[] args) {String S1=NewString ("Hello"); String S2=NewString ("Hello"); System.out.println (S1==S2);                System.out.println (s1.equals (S2)); String S3= "Hello"; String S4= "Hello"; System.out.println (S3==S4);                System.out.println (S3.equals (S4)); }}

This program is available: Because the string object was created using the New keyword initially, although the values are consistent (both "Hello"), it is still two separate objects. Use "= =" To determine the two objects, not only the value of the object is equal, and the address of the object is the same, so the first judgment is wrong; When you use "A.equals (b)" To determine two objects, you only need to determine whether the values of two objects are equal, so the second result is true And then two for assignment, the value of two objects is "Hello", so the values of two objects are equal to the address. Are true.

(1) in the string class, this overrides the implementation of the Equals () method: Comparing the current string object with the specified string object, the specified string object cannot be empty and the character sequence of the object is the same as the string sequence of the current string object, if these conditions are met, Then the two string objects are equal.

(2) If there is no string object with the same value as the string in the buffer pool, the virtual machine will create a new string object for that purpose, and store it in the string buffer pool, or if there is a string object with the same value as the string in the buffer pool. At this point, the virtual machine will not create a new string object for this purpose, and directly return a reference to the existing string object.

Three. Hands-on brain: The method of the string class can be called consecutively:

String str= "ABC";

String Result=str.trim (). toUpperCase (). Concat ("DEFG");

Please read the source code of the string class above in the JDK, imitate its programming method, write a MyCounter class, its method also supports the above "cascade" invocation attribute, the invocation example is:

MyCounter counter1=new MyCounter (1);

MyCounter counter2=counter1.increase, decrease (2). Increase (3);

......

 Public class Stringxunhuan {    publicstaticvoid  main (string[] args) {        //  TODO auto-generated method stub        String str= "abc";        String result=str.trim (). toUpperCase (). Concat ("DEFG");        SYSTEM.OUT.PRINTLN (result);}    }

classmycounter{intdata;  Public voidSetintd) {data=D; }     PublicMyCounter Increase (inti) {MyCounter a=NewMyCounter (); A.data=data+i; returnA; }     PublicMyCounter Decrease (intd) {MyCounter a=NewMyCounter (); A.data=data-D; returnA; }} Public classCounter { Public Static voidMain (string[] args) {//TODO auto-generated Method StubMyCounter counter1=NewMyCounter (); MyCounter Counter2=NewMyCounter (); Counter1.set (1); Counter2=counter1.increase (+). Decrease (2). Increase (3); System.out.println (counter2.data);//Note that this cannot be written as counter2, and the output is not selected as a hash value    }}

Four. After-school assignment 1: string encryption

The ancient Roman Emperor Caesar used the following methods to encrypt military information during the war:

Please write a program, using the above algorithm to encrypt or decrypt the user input of the English string requirements design ideas, program flowchart, source code, results.

1. Programming Ideas:

(1) First write a Zhuanhuan function, in the function of the use of the Charat function to read the individual characters, the non-X, y, z characters of the ASCII code value is added three, and the output of the changed character; X, Y, Z is the ASCII code minus 23 to get a,b,c.

(2) Call this function in the main function to output the result.

2. Program Flowchart:

3. Source code:

ImportJava.util.Scanner; Public classcodestring { Public Static voidMain (string[] args) {//TODO auto-generated Method StubSystem.out.println ("Please enter the English string:"); Scanner SC=NewScanner (system.in); String S=Sc.next ();        Sc.close ();    Zhuanhuan (S); }     Public Static voidZhuanhuan (String a) {CharCode;  for(intI=0;i<a.length (); i++)        {            if((A.charat (i)) <88) Code=(Char) (A.charat (i) +3);//Note: The subscript for Charat is also starting from 0            ElseCode=(Char) (A.charat (i)-23); System.out.print (Code+ " "); }          }}

4. Results:

Five. After-school assignment string encryption, hands-on Brain string.equals () method, sorting String class length (), CharAt (), GetChars (), replace (), toUpperCase (), toLowerCase ( ), Trim (), ToCharArray () usage notes, read notes published to the blog park

1.string.equals () Method: Compares the contents of two strings, with which the "= =" (The value and address of the object is judged) is distinguished.

2.Length (): Gets the length of the string.

3.charAt (): Gets the character at the specified position (where the subscript starts at 0).

4. GetChars (): Gets the substring copied from the specified position into the character array.

5.replace (): substring substitution.

6.toUpperCase (): All characters are capitalized to return a new string.

7. toLowerCase (): All characters become lowercase, returning a new string.

8.trim (): Remove the kinsoku space.

9.toCharArray (): Converts a String object to a character array.

A summary of the problems of 05-string and the experimental problems after class

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.