Strings are ubiquitous in program development and are the most frequent data types involved in user interaction, so strings are more than just our simple understanding of string str = "ABC"; Let's take a closer look.
In Java, strings are treated as string objects, and unlike basic data types, strings can directly use the methods of various string objects, but the basic data type is not, the string class is in the Java.lang package, and by default the package is automatically imported into all applications
The string object cannot be changed after it is created, it is immutable, we re-assign not the data that modifies the memory space referenced by the object, but the new object is created, but the previous object points to the new address, so the more times the string is re-assigned, the more useless garbage data in memory will be
Let's look at a simple example:
1 Public classHelloWorld {2 Public Static voidMain (string[] args) {3String S1 = "ABC";4String s2 = "abc";5 6String s3 = "I Love" +S1;7 8 //Compare strings S1 and S29System.out.println (is the S1 and S2 memory addresses the same?) "+ (S1 = =s2));Ten One //Compare strings S1 and S3 ASystem.out.println (is the S1 and S3 memory addresses the same?) "+ (S1 = =S3)); - -String s4 = "I Love" +S1; the //Compare strings S4 and S3 - //S1 is a variable, S4 knows the value at run time, so S3 and S4 are different objects -System.out.println (is the S3 and S4 memory addresses the same?) "+ (S4 = =S3)); -String S5 =NewString ("ABCD"); +String s6 =NewString ("ABCD"); - //Compare S5 and S6 +String.out.println (is the S5 and S6 memory addresses the same?) "+ (S5 = =S6)); A } at}
Analyze the running results, first know = = Compare the object in memory address, rather than the reference value, see S1 and S2, because S1 and S2 immediately appear, so the compiler will be optimized, S1 and S2 will point to the same memory area, so S1 = = S2 return True
After S3 assignment I love ABC, S3 is a new string object, so S1 = = S3 returns false
After assigning a value to S4, S4 is also an object, although the values of S3 and S4 are equal, but the system does not point them to the same memory address, so S3 and S4 are two separate objects, S3 = = S4 returns false
According to S1 = = S2 Returns true, presumably S5 and S6 also appear immediately thereafter, so should point to the same memory address, not, as long as the New keyword will be created as soon as possible, although the same content, so S5 = = S6 return False
Take a look at the common methods of the string class
For example, instantiating a string object: string ABC = "ABC"; You can invoke the specified method through ABC. Method (), the list of commonly used methods is as follows:
There are a few points to note:
1, character index of string str and array starting from 0, to Str.length ()-1
2, when using IndexOf lookup, if the match returns the position index, if not found then returns-1
3. When using the substring method to intercept a string, the string including the beginindex position, not including the Endindex position string
4, compare two string content is equal, you must use the Equals method, do not use = = to compare, said = = is compared to two string object to the first address of the memory address, but compare the character type and other basic data types can be used = = comparison Value
There are many ways to manipulate strings, and this requires more practice to enhance memory.
The string class has immutable properties, and when strings are frequently manipulated, there are many extra temporary variables, so using StringBuilder or stringbuffer can avoid these problems
StringBuffer is thread-safe, and StringBuilder is not thread-safe, so performance is higher than stringbuffer, typically creating a variable-content string object that takes precedence over the StringBuilder class, Common methods of this class are as follows:
The simple way to use it is as follows:
1 // Create a StringBuilder object 2 New StringBuilder ("Hello"); 3 str.append ("World"); 4 System.out.println ("str content:" + str + "Length:" + str.length ()); 5 Str.insert (5, ","); 6 Str.insert (11, "!" ); 7 System.out.println ("str content is:" + str);
The Append method is to append a string after a string, and unlike a string object, the original memory address is not preserved after the string is updated, thus saving memory space
The length method is consistent with string
Insert is the insertion of a character at a specified position, such as Str.insert (5, ","), a "," at index position 5, or a "," in the original position 5.
The ToString method converts the StringBuilder object to a String object, with an extra object in the program, converted by string str2 = Str.tostring ();
So the result of the program output is:
STR content is: HelloWorld Length: 10
STR content is: hello,world!
Understanding of the String object here
Further understanding of strings in Java