Looking back, Java restudying (eight): string of Java Basics

Source: Internet
Author: User

The string is the most frequently used class in Java, but it is not a primitive type, but rather a predefined string class to represent. As you can see from the source of the string class, string is implemented based on char[], and string in Java is an immutable string that cannot be changed after the string is initialized.

It is estimated that there is a question, the string can not be changed? The following two kinds are not all changes?

String a = "123";

A = "1234";

A = a + "F";

It should be noted here that the memory of the string is divided into two parts, the first memory heap in the contents of the specific content, that is, char[], followed by the memory stack is stored in the specific content of the address. The above-mentioned can not be changed for the part of the heap, and our usual string modification is to create a new char[in the heap, and then modify the stack to the address of the new char[], so the string changes in the C language compared to the content of the string in Java to modify the efficiency of a little bit less, At the expense of efficiency, the immutable strings can be shared.

We recommend that you use StringBuffer or StringBuilder instead of "+" in your daily development work, StringBuffer and StringBuilder create mutable strings and call ToString () after the modification is complete. You can turn it into an immutable string. StringBuffer and StringBuilder API are the same, the difference is that the former is thread-safe, the latter thread is not secure, and less thread synchronous processing in exchange for higher efficiency, Remember before the test seems to be StringBuilder efficiency is about three times times the StringBuffer, both of the speed is much higher than the "+" of the traditional stitching. Although the three ways of speed are processed at the millisecond level, there are still a few gaps in frequent calls, so it is strongly recommended to use StringBuffer and StringBuilder for string concatenation.

Another point is about the string comparison, the daily development of the string is often compared, the above mentioned in the memory of the string in the way, so there will be a case, the heap of two char[] content, but the location is not the same, using = = comparison found that the two do not want to wait, So string comparisons in Java need to use the Equals () method, which compares the contents of a string, regardless of where it is stored. The problem with the = = comparison is that there is an intermittent bug in the program, sometimes none, because two string objects point to the same char[] or two char[with the same content).

More information about string can be found in the API details.

Looking back, Java restudying (eight): string of Java Basics

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.