1. Each character in string is a 16-bit Unicode character. Unicode can easily express a wide range of international character sets, such as good Chinese support. Even Java identifiers can use Chinese characters, but no one can use them (only in a Tsinghua practical Java2 tutorial ).
2. Judge an empty string. Select one or their combination as needed
If (S = NULL) // from the perspective of reference
If (S. Length () = 0) // judge the slave Length
If (S. Trim (). Length () = 0) // whether there are multiple white spaces
The TRIM () method removes leading and trailing Unicode characters smaller than '"u0020' and returns a" trimmed "string. This method is very common. For example, if you need to enter the user name, the user accidentally adds leading or trailing spaces.ProgramWe should know that the user did not mean it, and even intentionally it should be handled intelligently.
It is a common operation to determine the null string, but the isempty () method is not provided until the Java class library is 1.6. Returns true only when length () is 0.
3. uninitialized, empty string "", and null. They are different concepts. Operations on uninitialized objects will be blocked by the compiler; null is a special initialization value and a reference that does not point to any object, an exception nullpointerexception is thrown during the operation of an object that is referenced as null. The empty string is a string with a length of 0, and the only difference between it and other strings is that the length is 0.
Example:
Public class stringtest {
Static string S1;
Public static void main (string [] ARGs ){
String S2;
String S3 = "";
System. Out. Print (s1.isempty (); // runtime exception
System. Out. Print (s2.isempty (); // compilation Error
System. Out. Print (s3.isempty (); // OK! Output True
}
}
4. There are many methods for the string class.CodeIt is advantageous to look at the JDK documentation. Otherwise, it is not worthwhile to spend a lot of time implementing an existing method, because writing, testing, and maintaining your own code increases the project cost, if the profit is reduced, the salary will not be paid out if it is serious ......
5. String comparison.
Java does not allow overload of custom operators. Therefore, strings must be compared with compareto () or comparetoignorecase (). S1.compareto (S2). If the returned value is greater than 0, the former is large. The former is equal to 0, which is generally large. The latter is smaller than 0. The comparison is based on the Unicode value of each character in the string.
6. tostring () method.
Any object in Java has the tostring () method, which is inherited from the object. Its function is to make the object look more meaningful in the output, rather than the strange memory address of the object. It is also helpful for testing.
7. The String object is unchanged! What can be changed is the reference of the string object.
String name = "Ray ";
Name. Concat ("long"); // string connection
System. Out. println (name); // output name, OK, or "Ray"
Name = Name. Concat ("long"); // assign the result of String object connection to Name Reference
System. Out. println (name); // output name, oh !, Changed to "raylong"
The preceding three statements generate three string objects: "Ray", "long", and "raylong ". The first statement does generate a "raylong" string, but does not specify to whom the string is referenced, so the name reference is not changed. The 3rd statements do not change "Ray" according to immutability. The JVM creates a new object and assigns the connection "Ray" and "long" to the name reference, therefore, the reference has changed, but the original object has not changed.
8. the immutability mechanism of string will obviously have a lot of redundancy in the String constant. For example, "1" + "2" + "3" +... + "N" produces N + (n + 1) string objects! Therefore, in order to use the memory more effectively, Java sets aside a special memory area called the "String constant pool ". Take care of string! When the compiler encounters a String constant, it checks whether the same String constant already exists in the pool. If it is found, point the new constant reference to the existing string without creating any new String constant object.
So there may be multiple references pointing to the same String constant. Is there any danger of Alias? No problem! The immutability of the string object ensures that the alias is not affected! This is a difference between a String object and a common object.
At first glance, this is the underlying mechanism, which has no impact on our programming. In addition, this mechanism will greatly improve the string efficiency, but this is not actually the case. When you connect n strings using strings, the time consumed is in the square level of n! The contents of each two strings are copied. Therefore, when processing a large number of string connections and requiring performance, we should not use string, stringbuffer is a better choice.
8. stringbuffer class. The stringbuffer class is variable and won't be in the String constant pool, but in the heap, won't leave a lot of useless objects. And it can safely use the string buffer for multiple threads. Each stringbuffer object has a certain capacity. As long as the length of the Character Sequence contained in the stringbuffer object does not exceed this capacity, no new internal buffer array needs to be allocated. If the internal buffer overflow occurs, the capacity increases automatically. The fixed capacity is 16 characters. I give thisAlgorithmThe name is "Tian meal algorithm ". I will give you a full bowl of rice. If it is not enough, I will give you a full bowl of rice.
Example:
Stringbuffer sb = new stringbuffer (); // The initial capacity is 16 characters
SB. append ("1234"); // This is 4 characters, so the capacity of 16 characters is sufficient, no Overflow
System. Out. println (sb. Length (); // The output string length is 4
System. Out. println (sb. Capacity (); // The buffer capacity of the output string is 16.
SB. append ("12345678901234567"); // This is 17 characters long and the capacity of 16 characters is insufficient. It can be expanded to 17 + 16 characters long.
System. Out. println (sb. Length (); // The output string length is 17.
System. Out. println (sb. Capacity (); // The buffer capacity of the output string is 34.
SB. append ("890"). Reverse (). insert (10 ,"-");
System. Out. println (SB); // output 0987654321-09876543214321
The length of the string and the size of the character buffer are two concepts. Pay attention to the difference.
Also, it seems cool to connect them in series! This conciseness and elegance can be achieved by concatenating return values.
10. stringbuilder class. The stringbuilder class is provided from j2se 5.0, which is similar to the stringbuffer class. Its value lies in its higher efficiency in string operations. The disadvantage is that thread security cannot be guaranteed and synchronization is not guaranteed. So what is the performance difference between the two? A lot!
See: http://book.csdn.net/bookfiles/135/1001354628.shtml
Practice:
The stringbuilder class is used for a single thread to improve efficiency, and its APIs are compatible with stringbuffer, which does not require additional learning costs and is inexpensive. Use stringbuffer for multithreading to ensure security.
11. String comparison.
The following may make you dizzy, so you can choose to read or not. It will not have any impact on your career. Keep in mind that you can use equals () to compare strings! Once "=" is used, it will be very strange. The reason why I put this part at the end is to save everyone's time, because this part is smelly and long. Three people are recommended: 1. No idle type. 2. If you want to thoroughly understand the Java string, it is useless even if you have learned it. 3. Like me, there are several ways to study the word "NLP.
In the old saying, the string is so special that some rules do not work on the string. I personally feel that this kind of particularity is not good. Example:
Example:
String str1 = "Java ";
String str2 = "Java ";
System. Out. Print (str1 = str2 );
Everyone on Earth who has a certain Java foundation knows that false will be output, because = compares references and equals compares content. You can run it on your own machine. The result is true! The reason is very simple. When the string object is put into the constant pool and the "Java" string is displayed again, JVM is excited to point the reference of str2 to the "Java" object, it thinks it saves memory overhead. It's not hard to understand.
Example B:
String str1 = new string ("Java ");
String str2 = new string ("Java ");
System. Out. Print (str1 = str2 );
I have read the previous example and learned how to be smart. This time, I will definitely Output True! Unfortunately, the JVM does not. The result is false. The reason is very simple. In Example A, the declared method is indeed to create a "Java" object in the String constant pool, but once the new keyword is displayed, JVM will allocate space for the string in the heap. The two declarations are totally different, which is why I put "How to Create a String object" in the future. Let's take a look at another example.
Example C:
String str1 = "Java ";
String str2 = "blog ";
String S = str1 + str2;
System. Out. Print (S = "javablog ");
Let's look at this example again. Many comrades dare not say whether it is true or false. People who love their brains will say "false ...... Congratulations! You will answer the question! Remove the word "it" and you will be completely correct. The reason is very simple. The JVM will indeed put the string object of the type such as string str1 = "Java"; in the String constant pool, but it does this at the time of compilation, string S = str1 + str2; it can only be known at runtime (we can see it at a glance, but Java must know it at runtime, and the structure of the human brain and computer is different ), that is to say, str1 + str2 is created in the heap, and s reference cannot point to the object in the String constant pool. The person who hasn't crashed will continue to look at Example D.
Example D:
String S1 = "Java ";
String S2 = new string ("Java ");
System. Out. Print (s1.intern () = s2.intern ());
What Is Intern? The result is true. If this method has never been used, and well-trained programmers will go to the JDK documentation. Simply put, the intern () method can be used to compare the content of a string with "=. Before I see the use of the intern () method, I think it is too much. In fact, I wrote a lot more than that. There are still many problems with the intern () method, such as the inconsistency of efficiency and implementation ......
Example E:
String str1 = "Java ";
String str2 = new string ("Java ");
System. Out. Print (str1.equals (str2 ));
Whether it is an object in the constant pool or heap, The equals () method is used to compare the content, which is so simple.