Java hands-on brain and after-school homework

Source: Internet
Author: User

1. View the results of the output. How do I interpret this output? What can you sum up from it?

Run results

True

True

False

Reason

1) in Java , the content of the same string constant ("Hello") only saves one copy to save memory, so s0, S1 , S2 the same object is actually referenced.

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

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

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

   1 S2) now refers to the same string object " a "!

  2) string The object's contents are read-only, using " + "Modify s1 The value of the variable is actually getting a new string object with the content " ab s1 < span> a "nothing, so, s1==s2 return false

3) the "ab" stringin the codeis a constant that refers to a string that is not related to the "ab" object referenced by s1 .

4)thestring.equals () method can compare the contents of two strings.

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;

}

How to use functions in the 3.String class

1.length ()

function: Gets the length of the string

String a= "ABC";

Int b=a.length ();

2.charAt ()

function: Gets the character at the specified position

String a= "ABC";

Char B=a.charat (3);

3.getChars ()

function: Gets the substring from the specified position copied into the character array

String s1= "ABC";

Char S2=new char[5];

S1.getchars (0,2,s2,0); Copy the 0,1,2 characters in S1 to the 0 -based position in the S2

4.Replace ()

role: substring substitution

String s1= "ABCD", S2;

S2=s1.replace ("AB", "12");

5.toUpperCase ()

Effect: Uppercase conversion

String s1= "ABC";

S1.touppercase () =abc

6.toLowerCase ()

role: Lowercase conversion

String s1= "ABC";

S1.tolowercase () =abc

7.Trim ()

function: Remove the leading and trailing spaces

String s1= "ABCD";

S1=s1.tirm ();

s1= "ABCD"

8.toCharArray ()

function: Converts a string object to a character array

String s1= "ABCD";

Char [] S2=s1.tochararray ();

4. String encryption

Design ideas

1. First Use the Scanner class to enter a string;

2. use the CharAt class to take out the characters in the string;

3. Add 3 to each character , then cast to char type, and assigned the literal value into an array of the defined char type;

4. determine whether the character is to think,X, Y, Z, if it is added 3 after minus the;

5. finally the output of the array contents of the defined char type;

Flow chart

Source

Import Java.util.Scanner;

public class Jiami {

public static void Main (string[] args) {

String s1= "";

System.out.println (" Please enter a string ");

Scanner sc=new Scanner (system.in);

S1=sc.next ();

Sc.close ();

Char[] S2=new char[s1.length ()];

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

{

S2[i]= (char) (S1.charat (i) +3);

if (S1.charat (i) >= ' x ' &&s1.charat (i) <= ' Z ' | | S1.charat (i) >= ' X ' &&s1.charat (i) <= ' Z ')

S2[i]= (char) (S1.charat (i) +3-26);

}

System.out.println ("The string after encryption is:");

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

System.out.print (s2[i]+ "");

}

}

Java hands-on brain and after-school homework

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.