Reading notes-string

Source: Internet
Author: User

"String" is the object that encapsulates the char[] array, consisting of three parts:

1, char array: It is a superset of the string represented by a string object;

2, 3, offset, and count, which represents the starting segment of a string object represented by the strings in a char array;

The string is specifically designed to contain the following three features:

1, invariance: Invariant mode immutable, saving the synchronization and lock waiting consumption;

2, for the "Constant Pool" optimization:

Variable memory space constant pool

String str1 = "abc" ——————— >

String str2 = "abc" ——————— > ABC

String str3 = new String ("ABC")--and str instance ——— >

Note: str1 = = Str3.intern () is TRUE;STR2, str2 directly points to the "ABC" in the constant pool and does not copy;

3, the final definition of the class;

Two construction methods for string:

public String(char value[]) {    this.value = Arrays.copyOf(value, value.length);}String(char[] value, boolean share) { this.value = value;}

The first construction method constructs a new char[] array;

The second construction method shares the original char[] array;

"Memory Leak" the construction method of a shared char[] array that is visible in the second package causes a memory leak

This construction method is a space-time-changing strategy (it is likely to be understood as a time-space, since share space, but most of the cases are GCS that block large spaces);

Calling this constructor will make the original char[] array unusable by the GC, if the original char[] is large, and the new string object references only a small part of it, which is the potential risk of substring (begin, end), Because this method uses the package-visible construction method to return a new string object that shares the old char[] array:

New String (offset + beginindex, endindex-beginindex, value);

The construction method that the package is visible, although the client code cannot be used, the constructor is called indirectly by calling the following method:

integer.tostring;

long.tostring;

String.Concat;

String.Replace;

string.substring;

String.ToLower;

String.ToUpper;

string.valueof;

Like what:

String str = new string (new char[10000000]);

String str2 = str.substring (1, 5);

1 million characters resident memory, cannot GC, because 5 strings are used;

"String Split"

String.Split provides the "regular expression" split feature:

"A;b,c:d". Split ("[; |,|:]");

StringTokenizer is an iterator that splits a string:

StringTokenizer st = new StringTokenizer (orignalstring, ";");

while (St.hasmoretokens ())

st.nextToken();

Use String.IndexOf and string.substring to split the string:

1234567891011121314
while (true) {    null;    Int J = orignalstr.indexof ('; ');    Break ; splitstr = orignalstr.substring (//update string}     

Split performance: Split < StringTokenizer < indexof&substring

"String Lookup"

IndexOf (char c) and charat (int index) are string-complementary methods;

String with Startwith and endswith;

Implement Startwith with Charat:

if (Origstr.charat (0) = = ' A ' && origstr.charat (1) = = ' B ' && Origstr.char (2) = = ' C ');

Implement EndsWith with Charat:

if (Origstr.charat (len-1) = = ' C ' && origstr.charat (len-2) = = ' B ' && origstr.char (len-3) = = ' A ');

Charat performance is higher than Startwith and endswith;

"String connection + + = concat StringBuilder StringBuffer"

The concatenated operation of a static string (string constant) is optimized by the compiler's direct operation at compile time:

String result = "ABC +" and "+" 123 "; Performance is higher than

StringBuilder builder = new StringBuilder ();

Builder.append ("abc");

...

Because the + operation of the static string is calculated at compile time, the runtime only has a string of "abcand123", not:

"ABC", "and", "123", "Abcand", "abcand123";

The connection operation of a dynamic string (string variable) is converted to a StringBuilder implementation by the compiler at compile time, and local variables in the loop are no exception:

So

String str1 = "abc";

String str2 = "and";

String STR3 = "append";

String result = str1 + str2 + str3;

Byte code is equivalent to:

String result = (new StringBuilder (String.valueof (STR1))). Append (str2). Append (STR3). toString ();

So, in fact, with + operation and StringBuilder performance is the same;

But the compiler is not smart enough to convert + to StringBuilder, often with too many new StringBuilder objects:

for (int i = 0; i < 10000; i++)

str = str + i;

will be converted by the compiler to:

for (int i = 0; i < 10000; i++)

str = (new StringBuilder(String.valueOf(str))).append(i).toString();

Therefore, you should explicitly use StringBuilder instead of relying on the compiler to convert to the StringBuilder implementation:

StringBuilder sb = new StringBuilder ();

for (int i = 0; i < 100000; i++)

sb.append(i);

StringBuffer is thread-safe, theoretically performing slightly lower than StringBuilder;

StringBuilder and StringBuffer are variable-length char[] arrays, and as with all variable-length arrays, specifying appropriate capacity can save on the cost of expansion and improve performance:

StringBuffer (int capacity) StringBuilder (int capacity);

Efficiency of string connections: StringBuilder >> concat > +

Reading notes-string

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.