Java Performance optimization strings

Source: Internet
Author: User

Code optimization for Java Program performance optimization

String part:

We need to know about this part of string StringBuffer Stringbulider

String is an immutable sequence of characters, optimized for constant pools, and the class is final

The invariance of string refers to the immutable character of a string object once created.

Constant pool optimization means that if two strings have the same sequence of strings, only one copy of the constant pool is available.

The string class is final, which means that the string class cannot be inherited or modified

The string actually consists of a three-part char array and an offset, and the string length consists of the actual character substring method is the three-part intercept

We take substring (beginindex,endindex) as an example in fact, when Beginindex==0 and endindex== the original string length will return the original string but in most cases this will not happen. When the original string is long, but the string to be truncated is short, the method intercepts and returns the string based on the offset, but at this point it is a copy of the original string and occupies the memory space. This is typically a space-for-time idea, but it can easily cause memory overflow

Revelation: So we use the new string (Str.substring ()) When we use the subString () function, to release a reference to the returned string to avoid a memory leak

There are a number of ways to create a memory leak with the string method, especially when you use it.

There are three methods of string efficiency segmentation, one of which is the spilt () method, and the use of the StringTokenizer class is not suitable for frequent use of the system.

You can also write your own method to split

This method uses the indexof () and SUBSTRING functions to find the position of the delimiter and then intercept the remaining string as a temporary string.

Proven method of practice three efficiency is the highest self can be tested under test scripts

What we want to know about string is that the Charat () method is efficient, and the Charat () method can be used instead of substituting the Startwith () method when using the

String performance Optimizations We'll talk about this for the time being.

Now let's compare the StringBuffer and StringBuilder.

StringBuffer uses lock synchronization and StringBuilder is not so stringbuffer thread-safe but StringBuilder more efficient, should be combined with the specific situation to use StringBuffer and StringBuilder

What we need to know about the above is that

The efficiency of the summation of string constants is actually higher because the compiler optimizes the accumulation at compile time, which is higher than the append operation of StringBuilder, but when the string variable is added, it is similar to StringBuilder's Append method operation.

Because the addition of variables is actually using StringBuilder for append

The reader chooses according to the actual oneself, one enhances the efficiency.

Java Performance optimization strings

Related Article

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.