If you have a harvest, please point to praise, or leave a message for encouragement. Thank you
String optimization.
1: Why to optimize strings
In development, there are many duplicate strings, strings are objects, these duplicate strings although the values are the same, but they are also a separate object, such a repetition of waste memory, so optimization.
2: How to avoid some mined area optimization
The premise Summary: string optimization Ah, is to put the string object in the constant pool, for everyone to quote, a= "ABC"; b= "ABC"; The ABC of A is placed in the constant pool, at which point B is also equal to ABC, and the same value is an ABC object. This is not a waste, so the ABC of B refers to the same ABC referenced by the Abc,ab in constant Pool A.
AB refers to the same abc,ab can not let them change, and you want to change will not change, make the string of char[] is final, and do not provide method modification, and cannot inherit write method modification, so you do not have to worry AB reference Abc,ab will pass the subscript to change the ABC value.
There are not only optimized strings in the constant pool, but also other, such as int a=3;int b=3; At the moment, a==b, presumably the value of this repetition is also optimized, specifically what, I also can not say.
3: How to do the optimization.
String a= "ABC"; String b= "abc"; at the moment ABC this string object was first placed in the constant pool for optimization, and a reference to Abc,b is also the ABC, so a==b,true.
String A1=new string ("abc"); At the moment this ABC is placed on the heap, giving A1 references from one address to the other, at which point string B1=new string ("abc");, A==b is not tenable, A's ABC object is an address, B's ABC object is a piece of address, not shared.
String A1=new string ("abc"); String B1=a;,a==b is set up, the ABC object of A is an address, b refers to a, which refers to the address of ABC, set up
A string is a class, and the methods in this class are:
Get the length, replace, intercept, to go to a space, a prefix comparison, indexof ("") returns the first occurrence of the character to the subscript, indexof ("", int) from the specified subscript continues to find the right and the subscript, LastIndexOf ("") returns the last found subscript , IndexOf ("", int) returns the last subscript, conversion, etc. from the specified position (left), as detailed in the API
But because it is immutable, this avoids duplication and reduces memory, but it is because of the immutable, if each string content is different. For example, in a loop
for (int a=0;a<100;aii) {
String B=a+i; }
This kind of live can not change 1 string is useless, should occupy or occupy memory, so appeared stringbuff ... The key words can't remember, the following explanation.
Tips:
Several construction methods for strings, new string (byet[]), new string (Byet[],int), new String (char[), etc., are described in the API,
It does not mean that you pass what parameters I convert to a string, but according to your value Ah, to code to find the corresponding, you can specify the encoding.
For example: byte[] a={97}; now new string (a), print a,byte[] a={9}; At the moment, new string (a), printed box, the middle is a question mark, commonly known as garbled, because you are not available for display.
String A = "Money oh ah Jkldjla";
Byte[] B = a.getbytes ();
A = new String (b, "GB2312");
System.out.print (a);
This, first converts the traditional simplified female to the byte array, the corresponding number is 27 88-59-74-80-95 106 107 108 100 106 108 97 so many, then in handing over the string, I now specify the GB2312 (can recognize the simplified)
Yes, the traditional is not recognized, and finally the result of a printed a is? X Oh Ah jkldjla, this, so ah, string object overloaded parameters, it is true that the byte array, character array can be restored to a string, but see if you meet. Restore, is a character conversion.