Action Brain (String)

Source: Internet
Author: User

1. Run the following sample code Stringpool.java to see its output. How do I interpret this output? What can you sum up from it?

Public class Stringpool {

Public Static void Main (String args[]) {

String s0= "Hello";

String s1= "Hello";

String s2= "He" + "Llo";

System. out. println (S0==S1);

System. out. println (S0==S2);

System. out. println (new string ("hello") = =new string ("Hello"));

}

}

Results:

Analysis:

When the compiler compiles a s2 sentence, it strips out the "+" sign, directly connecting the two strings to a single word ("Hello"). This optimization work is done automatically by the Java compiler. In Java, the same string constants ("Hello") with the same contents save only one copy to conserve memory so s0,s1,s2 actually refers to the same object in one memory space. When creating a string object directly using the New keyword, although the values are the same (all "Hello"), it opens up two different memory spaces, so it is still two separate objects.

Run the following program:

Public class TEXT5 {

Public Static void Main (String args[]) {

String s1= "a";

String s2=s1;

System. out. println (S1==S2);

s1+= "B";

System. out. println (S1==S2);

System. out. println (s1== "AB");

System. out. println (s1.equals ("AB"));

}

}

Results:

Analysis:

Assigning a value to a string variable means that two variables S1,S2 now refer to the same string object "a", so S1 is the same object as S2 before modifying S1, so the content of the True;string object is read-only, and the value of the S1 variable is modified with "+". is actually getting a new string object with the content "AB", which is independent of the object "A" referenced by the original S1, so the S1==s2 return value is false; 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. So the result is that the False;string.equals () method can compare the contents of two strings with "AB", so the return value is true.

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

Public boolean equals (Object anobject) {

if ( this = = AnObject)

{

return true;

}

if (AnObject instanceof String)

{

String anotherstring = (string) anobject;

int n = value. length;

if (n = = anotherstring. value. Length)

{

char v1[] = value;

char v2[] = anotherstring. value;

int i = 0;

while (n--! = 0)

{

if (V1[i]! = V2[i])

return false;

i++;

}

return true;

}

}

return false;

}

Analysis:

equals in the String class first compares the address, and if it is a reference to the same object, the object is equal, returns True, and if it is not the same object, the Equals method compares the characters within the two string object one after the other and returns true only if it is exactly equal, otherwise false.

3. The ancient Roman Emperor Caesar had 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) Design idea:

first enter a string, in the case of encryption, the first 23 lowercase letters or uppercase letters, then add 3, 3 lowercase letters or uppercase, then subtract 23. In the case of decryption, the first three lowercase or uppercase letters should be added 23, the other 23 letters should be reduced by 3. Each letter in the string can be found using the Charat () function to determine which letter can be implemented with the ASC code value.

(2) Program Flowchart:

(3) Source code:

Encrypt, Decrypt

Sun Qiwen, 2016.10.27

import Java.util.Scanner;

Public class Caesar {

Public Static void Main (String args[]) {

Scanner Scanner=new Scanner (System. in);

System. out. println (" Enter the action you want to perform: 1. decryption 2. encrypt ");

int n=scanner.nextint ();

if (n==1)

{

System. out. Print (" Please enter a string to encrypt:");

String String=scanner.next ();

String result = "";

Char Stringadd;

for (int i=0;i<string.length (); i++)

{

if (String.charat (i) >87&&string.charat (i) <91| | String.charat (i) >119&&string.charat (i) <123)

{

stringadd= (char) (String.charat (i)-23);

}

Else

if (String.charat (i) >64&&string.charat (i) <88| | String.charat (i) >96&&string.charat (i) <120)

{

stringadd= (char) (String.charat (i) +3);

}

Else

{

System. out. Print (" input Error! ");

break;

}

result = result + Stringadd;

}

System. out. Print (" encrypted result: "+result);

}

Else if (n==2)

{

System. out. Print (" Please enter a string to decrypt:");

String String=scanner.next ();

String result= "";

Char stringsub;

for (int i=0;i<string.length (); i++)

{

if (String.charat (i) >64&&string.charat (i) <68| | String.charat (i) >96&&string.charat (i) <100)

{

stringsub= (char) (String.charat (i) +23);

}

Else

if (String.charat (i) >67&&string.charat (i) <91| | String.charat (i) >99&&string.charat (i) <123)

{

stringsub= (char) (String.charat (i)-3);

}

Else

{

System. out. Print (" input Error! "); break;

}

Result=result+stringsub;

}

System. out. Print (" decryption Result: "+result);

}

Else

{

System. out. Print (" Please enter 1 or 2 to confirm the operation!") ");

}

}

}

(4) Results:

4. Defragment the String class length (), CharAt (), GetChars (), replace (), toUpperCase (), toLowerCase (), Trim (), ToCharArray () usage instructions.

(1) Length (): Gets the string length.

For example: String STR=ABCD;

Str. Length () = 4;

(2) charAt (): Gets the character at the specified position.

For example:str.charat (0) retrieves the first character in a string str,

Str.charat (Str.length ()-1) retrieves the last character of the string str.

(3) getChars (): Gets the string copied from the specified position into the character array.

Four parameters: 1) position of copy start,2) position of copy end, string value

3) The target string array,4) is the starting position of the target string array

For example:char[]s1={' i ', ' ', ' l ', ' o ', ' V ', ' e ', ' ', ' y ', ' o ', ' u '};

String s2 = new String ("you");

S2.getchar (0,4,s1,7);

System.out.println (S1);

The result is:I love you

(4) Replace (): string substitution.

through The replace () method of the String class, which replaces a character in the original string with the specified character, and gets a new string that is specifically defined as follows:

Public String replace (char Oldchar,char Newchar).

(5) toUpperCase (), toLowerCase (): English case conversion.

(6) Trim (): Remove the leading and trailing spaces.

(7) ToCharArray (): Converts a String object to a character array.

Action Brain (String)

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.