Java Programming Ideas: strings

Source: Internet
Author: User

The 1.String object is immutable, and every method in the string class that appears to modify a string value is actually creating a new string object that contains the modified content, so after modifying the string, you want to print a new value, either directly or by using a variable to accept , Direct printing of the original string reference does not change.

2. Connecting string strings via "+" is actually a compiler that automatically introduces the StringBuilder class, implemented through the Append () method.

3. When writing the ToString () method for a class, if the string operation is relatively straightforward, you can use "+" directly, relying on the compiler. However, if a loop is used in a method, it is best to create a StringBuilder object yourself and use it to manually construct the final result. If you use "+", the compiler will constantly create StringBuilder objects during the loop, affecting efficiency and causing excessive garbage. Although Java has an automatic recovery mechanism, but it is never real-time, but only when the memory is not enough to recover, so as far as possible to produce less garbage is also necessary.

4. In the ToString () method, do not use this directly, such as the use of a = "B" + this + "C" statement, the compiler does not recognize this when it will call the ToString () method, this time will fall into the recursion, and constantly error.

The 5.Formatter () class enables finer-grained control of string-string output formats, which can be referenced when needed.

6.string.format () method, the implementation principle is also the internal creation of formatter objects, the implementation of formatting.

7. Proficiency in regular expression writing specifications, while familiar with the use of the pattern and Matcher class API, the combination of the two functions is very powerful, almost can satisfy the operation of the string.

Java Programming Ideas: 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.