Drill down into Java strings

Source: Internet
Author: User

drill down into Java stringsJava String Class (Java.lang.String) is the most used class in Java, is also the most special class, many times, we are both familiar and unfamiliar. first, fundamentally understand the Java.lang.String class and the string poolFirst, I propose to look at the source code implementation of the String class, which is the fundamental starting point to understand the string class in essence. As you can see: 1, the String class is final and cannot be inherited. Public final class String. 2, the String class is the essence of the character array char[], and its value cannot be changed. Private final char value[]; then open the API document of the string class, you can see: 3, the String class object has a special way to create, that is, directly specify such as String x = "abc", "ABC" represents a String object. X is the address of the "ABC" object, also called a reference to the "ABC" object. 4. String objects can be concatenated by "+". A new string is generated after concatenation. It can also be concatenated by concat (), which is explained later. 6. The Java runtime maintains a string pool, Javadoc translation is very vague "string buffer". The string pool is used to hold the various strings produced in the runtime, and the contents of the strings in the pool are not duplicated. The normal object does not exist in this buffer pool, and the object created only exists in the stack area of the method. The following is a system memory: 5, there are many ways to create a string, summed up there are three categories: one, using the new keyword to create a string, such as String S1 = new String ("abc"), and second, directly specified. For example, string s2 = "abc"; third, a new string is generated using concatenation. For example, string s3 = "AB" + "C"; second, the creation of a string objectThe creation of string objects is also very important, the key is to understand its principle. Principle 1: When using any way to create a string object s, the Java runtime (the running JVM) will hold this x in the string pool to find if there is a string object of the same content, if not, create a string s in the pool, otherwise, not add in the pool. In principle 2:java, whenever you use the New keyword to create an object, you must create a new object (in the heap or stack area). Principle 3: Creating a String object using direct designations or concatenation with a pure string only checks the string in the maintenance string pool without creating one in the pool. However, the string object will never be created in the stack area again. Principle 4: Creating a String object with an expression containing a variable will not only examine the maintenance of the string pool, but also create a string object in the stack area. In addition, the Intern () method of string is a local method, defined as public native String intern (); The value of the Intern () method is to allow the developer to focus on the string pool. When the Intern method is called, if the pool already contains a string equal to this string object (which is determined by the Equals (object) method), the string in the pool is returned. Otherwise, this string object is added to the pool, and a reference to this string object is returned. third, recognize trim (), Intern () and concat (), "+". Iii. recognize spaces, empty strings, nullTake a look at the following example: publicclassstringtest {
Public Static voidMain (String args[]) {
//Create string Object "ABC" in Pool and heap, S1 point to object in heap
String S1 =NewString ("ABC");
//S2 Direct point to the pool object "ABC"
String s2 ="ABC";
//Create a new "ABC" object in the heap, S3 point to the object
String s3 =NewString ("ABC");
//Create the object "AB" and "C" in the pool, and S4 point to the object "ABC" in the Pool
String S4 ="AB"+"C";
//c pointing to the object "C" in the pool
String C ="C";
//Creates a new object "ABC" in the heap, and S5 points to the object
String S5 ="AB"+ C;

String s6 ="AB". Concat ("C");
String s7 ="AB". Concat (c);

System.out.println ("------------string-----------");
System.out.println (S1 = = s2);//false
System.out.println (S1 = = S3);//false
SYSTEM.OUT.PRINTLN (s2 = = s3);//false
SYSTEM.OUT.PRINTLN (s2 = = S4);//true
SYSTEM.OUT.PRINTLN (S2 = = S5);//false
SYSTEM.OUT.PRINTLN (s2 = = s6);//false
SYSTEM.OUT.PRINTLN (s2 = = s7);//false

String B1 =NewString ("");
String b2 = "";
String B3 =NewString ("");
String b4 = "". Intern ();
String B5 ="" + "";
String B6 =" ". Concat ("");
String B7 ="  ". Trim ();
String B8 ="  ";
String B9 ="    ". Trim ();

System.out.println ("------------empty string-----------");
System.out.println (B1 = = b2);//false
System.out.println (B1 = = B3);//false
System.out.println (B2 = = B3);//false
System.out.println (B2 = = b4);//true
System.out.println (B2 = = B5);//true*
System.out.println (B2 = = B6);//true*
System.out.println (B2 = = B7);//false*
System.out.println ("-----A----");
System.out.println (B2.equals (B7));//true
System.out.println (B7 = = B8);//false
System.out.println (B7 = = B9);//false
System.out.println (B7.equals (B9));//true
System.out.println (B9 = =NULL);//false

System.out.println ("B8.trim ():");
for(byteB:b8.getbytes ()) {
System.out.print (">>>"+ (int) B +" ");
}
System.out.println ("\nb8.trim ():");
for(byteB:b8.trim (). GetBytes ()) {
System.out.print (">>>"+ (int) B +" ");
}
System.out.println ("\nb9.trim ():");
for(byteB:b9.trim (). GetBytes ()) {
System.out.print (">>>"+ (int) B +" ");
}
}
} Iv. Common uses of string1, the string re-coding the problem is relatively simple, transcoding on a line to do, do not believe you see, but the question of why to Transcode, is a very abstruse problem, see example:/**
* String transcoding Test
*
* @author leizhimin 2009-7-17 10:50:06
*/
Public classtestencoding {
Public Static voidMain (string[] args)throwsunsupportedencodingexception {
System.out.println ("Before transcoding, the output Java System properties are as follows:");
System.out.println ("User.country:"+ System.getproperty ("User.country"));
System.out.println ("User.language:"+ System.getproperty ("User.language"));
System.out.println ("sun.jnu.encoding:"+ System.getproperty ("Sun.jnu.encoding"));
System.out.println ("file.encoding:"+ System.getproperty ("File.encoding"));

System.out.println ("---------------");
String s ="Lava Blog";
String S1 =NewString (S.getbytes (),"UTF-8");
String s2 =NewString (S.getbytes ("UTF-8"),"UTF-8");
String s3 =NewString (S.getbytes ("UTF-8"));
String S4 =NewString (S.getbytes ("UTF-8"),"GBK");
String S5 =NewString (S.getbytes ("GBK"));
String s6 =NewString (S.getbytes ("GBK"),"GBK");
System.out.println (S1);
SYSTEM.OUT.PRINTLN (S2);
System.out.println (S3);
System.out.println (S4);
System.out.println (S5);
System.out.println (S6);
}
Output: Before transcoding, the output Java System properties are as follows:
User.country:CN
User.language:zh
Sun.jnu.encoding:GBK
File.encoding:utf-8
---------------
Lava Blog
Lava Blog
Lava Blog
鐔 Corporation bo Å Argon
???????
Lava Blog

Process finished with exit code 0 came to a conclusion: A, turn a code, and use the code to build a string, is absolutely no garbled,----you the equivalent of no turn. B, transcoding or not, and the string itself encoding, the string itself is encoded with whom? ----The file encoding, or the encoding of your IDE settings. Here, I use the idea development tool, the default is UTF-8 encoding, but the operating system uses GBK, but no problem, I just follow UTF-8 to read my string will not be garbled. But the document is already UTF-8, you have to turn to GBK, not disorderly only blame! What's the way? Under Windows, open with Notepad or EditPlus and save as (and modify the encoding method). As for the conversion from UFT-8 to GBK, this depends on the internal code conversion tool, is a more complex problem, if anyone wants to study can tell me, a piece of research research. 2, character comparison is not a matching relationship? There are some types of string APIs that you can compare, and if not, you can find a regular expression to solve. 3. Get a character to get a sequence of characters ToCharArray(), and then just play it, Chinese is messy. 4, the interception of strings substring() 5, string substitution and lookup see http://lavasoft.blog.51cto.com/blog/62575/179324 6, beginning to end judgment StartsWith()/endwith ()7. String Sort comparison CompareTo (string anotherstring)
Compares two strings in a dictionary order.
Comparetoignorecase (String str)
Compares two strings in a dictionary order, regardless of case. 8, the string of equals () and Hashcode () has been implemented well, directly called, without rewriting 9, the type of string conversion too much, string. valueOf() series many. Similar to long. Parselong(Strings)10. Copying of strings copyvalueof() 11, Case conversion toLowerCase() toUpperCase()13. Regular matching http://lavasoft.blog.51cto.com/blog/62575/179324

Drill down into Java strings

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.