String type
In Java, a string is an unchangeable object. Each character in a string is a 16-bit Unicode character, because the Unicode character is 16-bit, therefore, Unicode can easily represent international character sets.
The following describes how to create three types of string objects:
1. String S = new string ();
S = "abcdef ";
2. String S = "abcdef ";
3. String S = new string ("abcdef ");
All three methods can create a String object. As mentioned earlier, string is an unchangeable object. Let's look at an example:
String S = "abcdef ";
String S2 = s;
S = S. Concat ("more stuff ");
System. Out. println ("s =" + S );
System. Out. println ("S2 =" + S2 );
The VM obtains the value of string S and adds "more stuff" to its end to provide us with an "abcdef more stuff ". Because the string is immutable, Vm cannot fill the new string in the old string referenced by S. Therefore, it creates a new String object for storage. So that s can reference this new object. Therefore, this example has two string objects: "abcdef" and "abcdef more stuff ".
String x = "Java ";
X. Concat ("rules! ");
System. Out. println ("x =" + x); // output "x = Java"
VM creates a new object to store "Java rules! ". But no variable references it. The second string object will be lost immediately and cannot be accessed. The referenced variable X still references the string with the original value of "Java.
Bytes ------------------------------------------------------------------------------------------------------------------------
Typical questions:
String S1 = "Spring ";
String S2 = S1 + "Summer ";
S1.concat ("fall ");
S2.concat (S1 );
S1 + = "Winter ";
System. Out. println (S1 + "" + S2 );
What is output? How many string objects are created in total? And several reference variables?
Answer:
Running result: Spring winter spring summer
A total of 8 string objects are created: "Spring", "Summer", "spring summer", "fall", "Spring fall ",
"Spring summer Spring", "Winter", "Spring Winter ".
Two referenced variables: S1 and S2
Bytes -----------------------------------------------------------------------------------------------------------------------
As the application grows, the string literal value occupies a large amount of program memory. For the program, the full string literal value usually has a large amount of redundancy. To make Java use memory more efficiently, JVM sets aside a special memory area called "String constant pool ". When the compiler encounters a string literal value, it first checks whether there is an identical String Literal Value in the pool. If yes, point the new reference to the existing string instead of creating a new String object. If this string is not in the String constant pool, Java will create a new String object in the regular memory and reference it, and this string will also be placed in the pool.
Important methods of the string class
1. Public char charat (INT index ). This method returns the characters at the specified index of the string. Remember that the string index value is calculated from 0. 2. Public String Concat (string S ). This method returns a string with the value of s after the string passed to this method.
3. Public Boolean restart slgnorecase (string S ). Whether the string and S of the call are the same, but true is returned if the characters are case-insensitive.
4. Public int length (). Returns the string length.
5. Public String Replace (char old, char new ). Replace the old character with the new character.
6. Public string substring (INT begin ). All characters starting from begin are truncated and returned.
7. Public string substring (INT begin, int end ). All characters starting from begin to end are truncated and returned.
8. Public String tolowercase (). Returns a string, all of which are converted to lowercase.
9. Public String touppercase (). Returns a string. All lowercase characters are converted to uppercase characters.
10. Public String trim (). Remove all spaces before and after the string.
Difference between stringbuffer and stringbuilder
The string object is immutable, So if you decide to perform a lot of processing on the string object. Then, a large number of discarded string objects are generated in the string pool. However, objects of the stringbuffer and stringbuilder types can be modified repeatedly without leaving a large number of discarded string objects.
Let's take a look at the use of stringbuffer and stringbuilder:
Stringbuffer sb = new stringbuffer ("ABC ");
SB. append ("def ");
System. Out. println ("SB =" + Sb );
---------------------------------------------------------------------
Stringbuilder sb = new stringbuilder ("ABC ");
SB. append ("def ");
System. Out. println ("SB =" + Sb );
In both examples, no additional objects are generated, and only one call to new is required.
Java. Lang. stringbuffer thread-safe variable character sequence. A string buffer similar to a string, but cannot be modified. Although it contains a specific character sequence at any time point, the length and content of the sequence can be changed by calling some methods. The string buffer can be safely used for multiple threads. These methods can be synchronized as necessary, so all the operations on any specific instance are in serial order, this sequence is consistent with the method call sequence of each involved thread.
The main operations on stringbuffer are append and insert methods. You can reload these methods to accept any type of data. Each method can effectively convert the given data to a string, and then append or insert the character of the string into the string buffer. The append method always adds these characters to the end of the buffer, while the insert method adds the characters at the specified point.
A variable character sequence of Java. Lang. stringbuilder is added to 5.0. This class provides
Stringbuffer-compatible APIs, But synchronization is not guaranteed. This class is designed as a simple replacement of stringbuffer, used when the string buffer is used by a single thread (this is common ). If possible, we recommend that you use this class first, because in most implementations, It is faster than stringbuffer. The two methods are basically the same.