JAVA string Jobs--hands-on brain and experimental questions after class

Source: Internet
Author: User

One: How to interpret such output? What can you sum up from it? 1. Source Code

mao2015.10.20

= = Two-resolution

in the case of raw data

for reference type variables

public class Stringpool {

public static void Main (String args[])

{

/*string s0= "Hello";

String s1= "Hello";

String s2= "He" + "Llo";

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

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

System.out.println (New string ("Hello") ==new string ("Hello"));//false

S0 S1 S2 is actually using the same object, all pointing to Hello This constant, all with Hello this address

New create memory space once created is several

*/

String S1 = "a";

String s2 = S1;

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

s1+= "B";

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

SYSTEM.OUT.PRINTLN (s1== "AB");//false

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

/* Assigning a string variable means: Two variables (S1,S2) now refer to the same strings object "a"!

String Span style= "font-family: the song Body;" > Modify the s1 The value of the variable is actually getting a new string object, Its contents are ab" S1 " a " s1==s2 return false < span>

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 .

The String.Equals () method can compare the contents of a two string. */

}

}

2.

3. Conclusion

Assigning a value to a string variable means: two variables(S1,S2)now refer to the same string object "a"! StringThe contents of the object are read-only, using the+the ModifyS1the value of the variable is actually getting a new string object with the content "AB", it was with the originalS1The referenced object "a"Nothing, So,S1==s2returnfalse; In the code, "AB"String is a constant that references a string to theS1referenced by the "AB"Object-independent. string.equals ()method to compare the contents of a two string.

S0 S1 S2 is actually using the same object, all pointing to the Hello constant, which is used by the Hello address.

Second, please see the string.equals () method implementation code, pay attention to learn its implementation method.

String.Equals (Object anobject)

Compares this string to the specified object.

For example , s1.equals (S2) is to compare S1 and s2 content

Third,String

the method of the string class can be called consecutively:string str= "abc"; String Result=str.trim (). toUpperCase (). Concat ("DEFG");// go to the end of the space, full capitalization, go to connector 1, source code

mao2015.10.23

continuous invocation of string

public class Stringconstant {

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);

}

}

2. Operation Result

(Please read the source code of the above method in the JDK String class, imitate its programming, write a MyCounter class, its method also supports the above "cascade" Invocation of an attribute, which is called an example:MyCounter counter1=new MyCounter (1); MyCounter counter2=counter1.increase. Decrease (2). Increase (3); )

Four, string encryption (please write a program, using the above algorithm to encrypt or decrypt the user input of the English string.) )

1. Source Code

mao2015.10.23

String Encryption

public class Secret {

public static void Main (string[] args) {

TODO auto-generated Method Stub

String S1 = "FDSAJFJJ";

Char a[] = new Char[s1.length ()];

S1.getchars (0, S1.length (), a, 0);

Char b[] = new Char[s1.length ()];

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

{

B[i] = (char) (A[i] + 3);

}

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

System.out.println (B[i]);

}

}

2. Operation Result

JAVA string Jobs--hands-on brain and experimental questions 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.