JAVA basics -- Re-recognize String strings, and java re-recognize string

Source: Internet
Author: User

JAVA basics -- Re-recognize String strings, and java re-recognize string
In-depth analysis of Java String

Strings are everywhere in program development. For example, the user name and password entered during user login use strings.

In Java, strings are processed as String objects. The String class is located in the java. lang package.By default, this package automatically imports all programs.

There are three methods to create a String object
String s1 = "I am String 1"; String s2 = new String (); // create an empty String object String s3 = new String ("I am String 2 "); // create an empty string object

Create a String objectNote:The following question:

1 // declare a string type s1, that is, there is no memory address requested, and there is no reference address in the memory; 2 String s1; 3 // declare a string type s2, at the same time, an address is requested in the memory, but the address does not point to any reference address; 4 String s2 = null; 5 // declare a string type s3, the address is requested in the memory and points to a reference address that references the String. 6 String s3 = ""; 7 // Similarly, s3; 8 String s4 = new String ();

Although the value of new String () is the same as that "",Different memory addresses.

In general, it is best to use the String str = ""; statement to prevent exceptions that cannot be found due to confusion of the reference address of subsequent programs! String s = null; String s; less powerful! Don't use it if you don't need it!

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

Immutability of String strings

The String object cannot be modified after it is created. YesImmutableThe so-called modification actually creates a new object and pointsDifferent memory space. As follows:

1 public static void main (String [] args) {2 // TODO Auto-generated method stub 3 // assign values to the four String variables respectively, 4 String s1 = ""; 5 String s2 = ""; 6 String s3 = new String (" "); 7 String s4 = new String (""); 8 9 // The String that appears multiple times. The java compiler creates only one memory space and returns the true10 System. out. println (s1 = s2); 11 // s1 and s3 are different objects, so false12 System is returned. out. println (s1 = s3); 13 // s3 and s4 are different objects, so false14 System is returned. out. println (s3 = s4); 15 16 17 s1 = "Welcome to" + s1 + ""; 18 // The string s1 is modified to point to the new memory space, returns false19 System. out. println (s1); 20 21 System. out. println (s1 = s2); 22 23}

Running result:

Result Analysis:

Based on the above Code and running results, we will analyze them one by one. Why is such a result? How is the immutability of String strings reflected?

1. A String object is declared through String s1 = ""; s1 stores the reference of the String object and the reference relationship in the memory is shown in:

Then, through s1 = "Welcome to" + s1 + "blog"; changed the string s1, in fact, the essence isA New String object is created., Variable s1 points to the newly created String object, as shown in:

2. Once a string is created in memory, the string cannot be changed. If you need a variable string, you can use StringBuffer or StringBuilder. (I will write a blog post tomorrow to explain how to use them ~).

3,Each time a new string is created, a new object is generated., Even if the content of the two strings is the same, it is "false" when "=" is used for comparison. If you only need to compare whether the content is the same, you should use "equals () "method (previously mentioned in the conditional operators section ~~).

Common methods for the String class in Java I

The String class provides many methods to process strings, such as obtaining String lengths, intercepting strings, converting strings into uppercase or lowercase letters, and splitting strings, let's take a look at its power.

Common Methods of the String class:

Wow, there are many common methods for the String class! It is hard to remember. We can use the code to familiarize ourselves with the usage of the method:

1 public static void main (String [] args) {2 // TODO Auto-generated method stub 3 String str = "Learning java programming "; 4 5 // print the string length 6 System. out. println ("String Length:" + str. length (); 7 8 // locate the 'string' position. If no value is found, the return value is-1 9 char c = 'string'; 10 System. out. println ("character 'string' position:" + str. indexOf (c); 11 System. out. println ("Location of the character 'B':" + str. indexOf ('B'); 12 // find the location of the string "java". If no value is found, the System-113 is returned. out. println ("string" java "location" + str. indexOf ("java"); 14 System. out. println ("the location of the string" cloud-based summer "+ str. indexOf (""); 15 16 // split the String into an array by space and output array element 17 String [] arr = str. split (""); 18 System. out. print ("split the string into an array by space" + Arrays. toString (arr); 19 System. out. println (); 20 // Press (which does not exist in the String) to split the String into an array and output the array element 21 String [] arr2 = str. split (","); 22 System. out. print ("Press, (which does not exist in the string) to split the string into an array" + Arrays. toString (arr2); 23 System. out. println (); 24 // split the String into an array by space, specify the maximum length of the array to 2, and output the array element 25 String [] arr3 = str. split ("", 2); 26 System. out. print ("split the string into an array by space and specify the maximum length of the array as 2:" + Arrays. toString (arr3); 27 System. out. println (); 28 29 // obtain the substring 30 System between the index location [3, 7. out. println ("obtain the substring between index locations [3, 7):" + str. substring (3, 7); 31 // obtain the substring starting with index position 3 32 System. out. println ("obtain the substring between index locations [3, 7):" + str. substring (3); 33 34 35}

Running result:

Result Analysis:

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

2. When indexOf is used for character or string search, if a match is returned, the location index is returned. If no match is returned,-1 is returned.

3. When using substring (beginIndex, endIndex) for string truncation, including the beginIndex position, excluding the endIndex position

4. Use String [] split (String regex, int limit) to split a String. If the String is not separated by characters, that is, the returned array with a length of 1 is the original string.

Common methods for the String type in Java II

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

1 public static void main (String [] args) {2 // TODO Auto-generated method stub 3 String str = "Learning java programming "; 4 5 // convert characters to uppercase 6 System. out. println ("converts characters to uppercase:" + str. toUpperCase (); 7 // rewrite string to lowercase 8 System. out. println ("to convert the string to lowercase:" + str. toLowerCase (); 9 10 // get the character 11 System at the position of 1. out. println ("Get the character at 1:" + str. charAt (1); 12 13 // convert string into byte [] array, and print the output 14 byte [] B = str. getBytes (); 15 System. out. print ("Convert string to byte [] Array : "+ Arrays. toString (B); 16 System. out. println (); 17 18 // define a new String, followed by a space 19 String str2 = "Learning java programming "; 20 // return the String 21 String str3 = str2.trim (); 22 System. out. println ("returns the string that removes leading and trailing spaces:" + str3); 23 24 // compares str and str3 strings to 25 System. out. println ("is the memory address of str and str3 the same? : "+ (Str = str3); 26 System. out. println (" is the content of str and str3 the same? : "+ Str. equals (str3); 27 28} 29 30}

Running result:

Result Analysis:

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

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

3. the byte value corresponding to Chinese characters is negative because each byte is 8 bits and the maximum value cannot exceed 127.The number of characters after being converted to byte exceeds 127. If the number is exceeded, it is displayed as a negative number.(For coding, I will explain it in the follow-up blog. I hope it will be nice ~~)

Note: The equal () method in the Object class compares whether the Object reference points to the same memory address, while equals () in the String class (): compare whether the content stored in two string objects is consistent.The Object class is the equal () method; the String class is equals (), which is a second different.

 

For such a complicated method, we recommend that you use one of the following learning skills:Poor memory! When you look at it, you must try again ~~

 

 

Related Article

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.