String strings are read-only, immutable
To view the source code of the string class, you can find that the string class is decorated with the final keyword;
In addition, you can also see the string class source code in other ways to implement, any can modify the string value method, such as String concatenation method Concat (String str), the return is a new string object, rather than on the original string object modification, The code is as follows:
public string concat (String str) {int otherlen = Str.length (); if (Otherlen = = 0) { return this ; int len = Value.length; char buf[] = arrays.copyof (value, len + Otherlen); Str.getchars (buf, Len); return new String (buf, true ); }
In the above code, the original character array and the new string array are copied to a new character array, and new is returned as a string object;
Overloading "+" with StringBuilder
Let's take a look at the following code that uses the "+" stitching string:
Packagestrings; Public classTest { Public Static voidMain (string[] args) {String a= "Test"; String b= A;//B holds a reference to a//here the compiler will automatically create the StringBuilder object for us and call the Append method,//finally call the ToString method of StringBuilder to return a new string objectA = a + "1" + "2" + "3";//a new string object is generated to assign aSystem.out.println (b==A);//reference a points to a new string object, not equalSystem.out.println (a); }}
When using the "+" stitching string, the compiler will automatically create a StringBuilder object for us, and call the Append method to stitch the string, and finally call StringBuilder's ToString method to return a new string object;
Perhaps you think that since the compiler will create an automatic StringBuilder object for us can use the "+" operator, in fact, the compiler in some cases, the extent of optimization for us is not enough, the following code example, in the loop body using the "+" operator;
package strings; public class Test { public static void main (string[] args) {String A = "" ; for (int i = 0; I < 5; I++ // This produces two temporary objects a +=i; } System.out.println (a); }}
You can use Eclipse debugging to enter the source code, you will find that the compiler will generate a StringBuilder object each time, and call the ToString method to generate a new string object, that is. Each loop body executes once, resulting in two temporary objects, it can be seen that when the number of cycles is large, there will be a lot of garbage collection of intermediate objects, and the direct use of StringBuilder there is no such situation, the following code, for a correct example:
Package strings; Public class Test { publicstaticvoid main (string[] args) { new StringBuilder (""); for (int i = 0; I < 5; i++) { a.append (i); } System.out.println (a); }}
In short, as long as the operation involved in string, choose StringBuilder is always right;
Unconscious recursive invocation
When overriding the ToString method, if you do not pay attention to the use of the This keyword, it is possible to call into a recursive invocation of the trap, the following code:
Package strings; Public class Test { @Override public String toString () { returnthis ; } Public Static void Main (string[] args) { new Test (); System.out.println (t); }}
When executed, the Stackoverflowerror exception is thrown, because the "addr:" + this statement in the ToString method calls itself the ToString method, resulting in an endless recursive call, then a stack overflow, throwing an exception;
If you just want to print the address of the object, you can call the Super.tostring () method, because the ToString method of the object objects calls the Hashcode Print object address by default;
Methods of the Sting class
About the methods in the string class, you can view the string source code or JDK API documentation, relatively well understood, so-called string object, is essentially a character array;
The methods in the string class are mostly implemented by manipulating a char value[] array maintained inside a string;
The main methods are as follows, picture reference from Java programming Idea-4:
String formatted output
For formatted output, here are a few examples:
Format Method : Can be used for printstring and pritwriter, such as System.out.format, if you have the use of C-language printf syntax experience, learning format syntax will be very easy, basically similar, Here is a simple example:
System.out.format ("%5d:%2f", 101,1.131452);
Formatter class : A printf-style format string interpreter, as in the following example:
Package strings; Import Java.util.Formatter; Public class Test { publicstaticvoid main (string[] args) { new Formatter (system.out); Formatter.format ("%5d:%2f", 101, 1.131452); Formatter.close (); }}
String.Format method : A static method of the String class, which is actually implemented internally by creating a formatter object;
System.out.println (String.Format ("%5d:%2f", 101, 1.131452));
Regular expressions
The specific syntax for regular expressions is not detailed, in the string class, the following methods are mainly involved in regular expressions:
Matches: Determines whether the specified regular expression rule is matched
Split: Split string
Replaceall/replacefirst: Replaces the matched character with the specified string
The following code is a simple example of use:
Packagestrings; Public classRegextest { Public Static voidMain (string[] args) {//Match NumbersSYSTEM.OUT.PRINTLN ("1314". Matches ("\\d+"))); //Divide by numberstring[] Splitarr = "asdashh45hiu9jkjaks54d". Split ("-?\\d+"); for(String Str:splitarr) {System.out.print (str+ ", "); } //Replace Number as * numberSystem.out.println (); System.out.println ("Asdashh45hiu9jkjaks54d". ReplaceAll ("-?\\d+", "*")); }}//Output//true//Asdashh, Hiu, Jkjaks, D,//asdashh*hiu*jkjaks*d
"Java Basics" string