String performance optimization in Java _java

Source: Internet
Author: User

Instead of using string constructors, strings can be used directly.

Two special cases:

1) to convert char [] to a string,
(2) using a substring () method of a large string object;

String.Equals () is faster than string.equalsignorecase ();

Try to use StringBuilder to construct a string instead of the "+" operator and String.Concat () (unless it is an expression, String s = a + B + c);

StringBuilder is not synchronized (not synchronized), so it is faster than stringbuffer;

In string[buffer| Builder] constructor, because creating a buffer that is too small can degrade performance;

String.Length () ==0 is faster than String.Equals (""). Use Java 6 o'clock, string.isempty () faster;

Calling String.tostring () is meaningless;

Because a string is immutable, all of the string's methods return a new instance by returning the modified string.

The String.Split (regex) actually simply invokes the Pattern.compile (Regex). Split (this, limit), and each compile () returns a new pattern. So if you call split frequently, it's best to create a single pattern instance and reuse it instead of split ().

The above is to share the content of this article, I hope you can enjoy.

Please take a moment to share the article with your friends or leave a comment. We will sincerely thank you for your support!

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.