Java Classroom _ Hands-on brain 4

Source: Internet
Author: User

1. Please run the following sample code Stringpool.javato see its output. How do I interpret this output? What can you sum up from it?

Answer: In in Java, the same string constants ("Hello") with the same contentssave only one copy to conserve memory, so s0,s1, S2 is actually referring to the same pair of

like. when the compiler compiles a s2 sentence,it strips out the "+" number and connects the two strings directly to a string ("Hello"). This optimization is done by the Java

The compiler finishes automatically. When you create a string object directly using the new keyword, the value is the same (all "Hello"), but it is still two separate objects.

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

A : Assigning values to a string variable means: two variables (S1,S2) now refer to the same string object " a "!

The contents of the String object are read-only, and thevalue of the S1 variable is modified with "+" , in effect, a new string object is obtained.

its content is " AB ", it was with the original S1 The referenced object " a "Nothing, So, S1==s2 return false ; In the code, " AB

"String is a constant that references a string to the S1 referenced by the " AB "Object-independent. the string.equals () method can

Compares the contents of a two string.

2. See The implementation Code of the String.Equals () method, and learn how to implement it.

Code:

public class Stringequals {

public static void Main (string[] args) {

String S1=new string ("Hello");

String S2=new string ("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));

}

}

Results

Analysis: use the Equals () or equalsignorecase () method to compare whether the contents of the two strings are the same, using the = = Compares whether a two-character string variable refers to the same

string Object. s1 and s2 respectively applied for the respective string object with new, so the system output is false .

3. Usage:

1)length (): Get string length

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

3)getChars (): gets the substring from the specified position copied to the character array (it has four parameters, as described in the example);

4)replace (): substring substitution

5)touppercase (), tolowercase (): Case Conversion

6)trim (): Remove the Kinsoku space:

7)tochararray (): Converts a String object to a character array

Explain

Define a A string of type a,a.length () that represents the length of the string a ;

use for (int i=0;i<a.length (); i++) to give a designator Atchar (i) to indicate a The character of the first I;

Chararray = new char[5];

A.getchars (0, 5, chararray, 0);

Four The meaning of a parameter

1. The starting position of the copied character in the string

2. The last character to be copied is added to the subscript in the string. 1

3. Target character Array

4. The copied character is placed in the starting subscript of the character array,

Repalace (A, b ) with a replacement

A.touppercase () \a.tolowercase () are lowercase to uppercase, uppercase to lowercase;

A.trim () is to remove the word string a 's leading and trailing spaces;

A.tochararray () converts the string A to a character array.

public class Az

{

public static void Main (string[] args)

{

TODO auto-generated Method Stub

String A= "This is my original string, it's very good! ";

String b= "";

Char A1[]=a.tochararray ();

for (int i=a.length ()-1; i>=0; i--) {

B + = A1[i];

}

String c=a.touppercase ();

System.out.println (b + "\ n" +c);

String r = "your";

A = A.replace ("My", r);

System.out.println (A.trim ());

}

}

4, String str= "abc";

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

Please read in the JDK, the String Class of the above methods of the source code, imitating its programming, writing a MyCounter class, its methods also support the above "cascading" invocation characteristics, the invocation example is:

MyCounter counter1=new MyCounter (1);

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

public class MyCounter

{

public static void Main (string[] args) {

TODO auto-generated Method Stub

String str= "ABC";

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

SYSTEM.OUT.PRINTLN (result);

}

}

Results:

After class homework one: 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.

Design idea: Enter the substring to encrypt str, define the length of the string, convert the string to a single character, +3 per character ,

Move Backward 3 One, define Output , multibyte each new word to Output , the string is the encrypted string after which it is eventually output.

Program Flowchart:

Source:

Import Java.util.Scanner;

public class Saa

{

public static void Main (string[] args)

{

TODO auto-generated Method Stub

System.out.println (" Please enter character ");

Scanner in=new Scanner (system.in);

String Key=in.nextline ();

int L=key.length ();

Char A;

System.out.println (" after encryption:");

for (int i=0;i<=key.length () -1;i++)

{

A=key. (i);

if (a< (char) ' x ') System.out.println ((char) (a+3));

if (a== (char) ' x ') System.out.println (' a ');

if (a== (char) ' Y ') System.out.println (' B ');

if (a== (char) ' z ') System.out.println (' C ');

}

}

}

Results:

Java Classroom _ Hands-on brain 4

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.