Java Basics--re-recognizing string strings

Source: Internet
Author: User

Deep parsing of Java string strings

Strings are everywhere in the development of a program, such as the user name, password, etc. that are entered when the user logs in.

In Java, strings are treated as objects of type string. The String class is located in the Java.lang package. by default, the package is automatically imported into all programs.

There are three ways of creating a String object
String s1= "I am string 1"; string S2=new string (); // to create an empty string object String s3=New string ("I am string 2"); // to create an empty string object

To create a string object, note The following question:

 1  //  Declares a string type of S1, which does not have a memory address in the application, or any point in memory to the reference address;  2  string S1;  3  //  Declares a string type of S2 and applies an address in memory, but the address does not point to any reference address;  4  String s2=null  ;  5  //  Declares a string type of S3, which applies an address in memory that points to a reference address that references the string;  6  string s3= ""  7  //  similarly S3;  8  string S4=new  String (); 

Although the new String () is the same as the value, the memory address is not the same .

It is generally best to use string str = "" for strings, and to prevent exceptions that are not found by the subsequent program because of a confusing reference address! String s = null; String s; use less strength! No need to use it!

>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>

The invariance of string strings

A String object cannot be modified after it is created, and is immutable , so-called modification is actually creating a new object that points to a different memory space . As shown below:

1  Public Static voidMain (string[] args) {2         //TODO auto-generated Method Stub3         //assign a value of four string variables, the same content4String s1= "cloud-opened Li Xia Zhong";5String s2= "cloud-opened Li Xia Zhong";6String s3=NewString ("cloud-open Li Xia Zhong");7String s4=NewString ("cloud-open Li Xia Zhong");8         9         //multiple occurrences of the string, the Java compiler creates only one memory space, so returns TrueTenSystem.out.println (s1==S2); One         //S1 and S3 are different objects, so return false ASystem.out.println (s1==S3); -         //S3 and S4 are different objects, so return false -System.out.println (s3==S4); the          -          -S1= "Welcome to the +s1+" blog Park "; -         //The string S1 is modified, pointing to the new memory space and returning false + System.out.println (S1); -          +System.out.println (s1==S2); A          at}

Operation Result:

Results Analysis:

Combined with the above code, and the results of the operation we have to analyze each, why this result? How does the invariance of string strings manifest?

1, through the String s1= "cloud-opened Li Xia Zhong"; Declares a string object that S1 a reference to a string object, storing a reference relationship in memory as shown in:

Then through the s1= "Welcome to" +s1+ "blog Garden"; Changing the string s1, essentially creating a new String Object , the variable S1 points to the newly created string object, as shown in:

2. Once a string is created in memory, the string will not be changed. If you need a string that can be changed, we can use StringBuffer or StringBuilder ( I will write a blog tomorrow to explain their use, please pay attention to Oh).

3, each time new a string is to produce a new object , even if the contents of the two strings are the same, use "= =" to compare the "false", if you only need to compare the content is the same, you should use the "Equals ()" Method (the preceding conditional operator section said Oh ~ ~).

Common methods for String classes in Java Ⅰ

The string class provides a number of methods to handle strings, such as getting the length of a string, intercepting a string, converting a string to uppercase or lowercase, string splitting, and so on, and let's take a glimpse of its power.

Common methods of the String class:

Wow, there are a lot of common methods of the string Class! Rote memorization is not possible, let's combine the code to familiarize yourself with the use of the method:

1  Public Static voidMain (string[] args) {2         //TODO auto-generated Method Stub3String str= "Learning Java Programming";4         5         //Print String length6System.out.println ("String length:" +str.length ());7         8         //find the position of the character ' codec ' if it is not found return-19         Charc= ' compilation ';TenSystem.out.println ("character ' edit ' position:" +Str.indexof (c)); OneSystem.out.println ("character ' B ' position:" +str.indexof (' B '))); A          //find the location of the string "Java" If no return-1 is found -System.out.println ("The location of the string" Java "+str.indexof" ("Java")); -System.out.println ("string" Li Xia zhong "position" +str.indexof ("cloud-open Li Xia Zhong")); the          -          //splits a string into an array by space and outputs the array elements -String[] Arr=str.split (""); -System.out.print ("Split the string into an array by space" +arrays.tostring (arr)); + System.out.println (); -         //Press, (not present in the string) to split the string into an array and output the array elements +String[] Arr2=str.split (","); ASystem.out.print ("Press, (not present in string) splits the string into an array" +arrays.tostring (ARR2)); at System.out.println (); -         //splits the string into an array by space, and specifies that the array has a maximum length of 2 and outputs the array elements -String[] Arr3=str.split ("", 2); -System.out.print ("Splits the string into an array by space and specifies that the maximum length of the array is 2:" +arrays.tostring (ARR3)); - System.out.println (); -          in         //get sub-string between index position [3,7] -System.out.println ("Get the substring between index locations [3,7]:" +str.substring (3, 7)); to         //gets the substring starting at index position 3 +System.out.println ("Get the substring between index locations [3,7]:" +str.substring (3)); -  the  *}

Operation Result:

Results Analysis:

1. The index of characters in the string Str starts from 0 and ranges from 0 to Str.length ()-1

2. When using IndexOf for character or string lookups, if the match returns a position index, or 1 if there is no matching result

3. When using substring (beginindex, endIndex) for string interception, include characters from the beginindex position, excluding characters from the EndIndex position

4, using string[] Split (string regex, int limit) for string splitting, if the string is not separated by a character is not delimited , that is, the return length of 1 of the array content is the original string.

Common methods for String classes in Java Ⅱ

Let's continue to look at the commonly used methods of the String class, as shown in the following code:

1      Public Static voidMain (string[] args) {2         //TODO auto-generated Method Stub3String str= "Learning Java Programming";4         5         //convert a character to uppercase6System.out.println ("Convert character to uppercase:" +str.touppercase ());7         //then the string is converted to lowercase .8System.out.println ("re-speak string converted to lowercase:" +str.tolowercase ());9         Ten         //gets the character with a position of 1 OneSystem.out.println ("Gets the character at position 1:" +str.charat (1)); A          -         //Convert string to byte[] array and print output -         byte[] b=str.getbytes (); theSystem.out.print ("Convert string to byte[] array:" +arrays.tostring (b)); - System.out.println (); -          -         //define a new string, add the above and after +String str2= "Learning Java Programming"; -         //returns a string that removes a space before and after +String str3=Str2.trim (); ASystem.out.println ("Returns a string that removes spaces before and after:" +STR3); at          -         //Compare str and STR3 strings -System.out.println ("Str and STR3 have the same memory address?") : "+ (str==str3)); -System.out.println ("Str and STR3 have the same content?") :"+str.equals (STR3)); -          -     } in  -}

Operation Result:

Results Analysis:

1. = =: Determine whether the first address of the two strings in memory is the same, that is, determine whether the same string object.

2. Equals (): Compares whether the content stored in two string objects is consistent.

3. The byte value corresponding to the Chinese character is negative, because each byte is 8 bits, the maximum value cannot exceed 127, and the Chinese character is converted to more than 127 bytes, if it overflows, it is displayed as a negative number. ( about the code, I will in the follow-up blog post explanation, little look forward to Oh ~ ~)

Note: the equal () method in the object class compares whether the object's reference points to the same memory address , while the String Class equals (): Compares the content stored in the two string object for consistency. The object class is the equal () method, and the String class is Equals (), which has a difference of s.

For such a complex method, we recommend a learning skill: good memory than bad pen! See more at the same time must be more knock Oh ~ ~

Java Basics--re-recognizing string strings

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.