Java Basics: String, StringBuilder, StringBuffer

Source: Internet
Author: User
Keywords java basics java string java stringbuffer

1. String

Simple Application Server
USD1.00 New User Coupon
* Only 3,000 coupons available.
* Each new user can only get one coupon(except users from distributors).
* The coupon is valid for 30 days from the date of receipt.

The value of String is immutable, so every operation on String will generate a new String object, which is not only inefficient, but also wastes a lot of limited memory space
public class TestMain {
     public static void main(String[] args) {
         String str0 = "123";
         String str1 = "123";
         System.out.println(str0 == str1);//true

         String str2 = new String("234");
         String str3 = new String("234");
         System.out.println(str2 == str3);//false

     }
 }

There is an area in the JVM called the constant pool. The data in the constant pool is some data that is determined during compilation and saved in the compiled .class file. In addition to including all 8 basic data types (char, byte, short, int, long, float, double, boolean), there are constant values of String and its arrays, and some symbolic references in text form.

The Java stack is characterized by fast access speed (compared to the heap block), but the space is small, the data life cycle is fixed, and it can only survive until the end of the method, and the data is shared among them
String str0 = "123", when compiling, a constant "123" is created in the constant pool, str1 will first go to the constant pool to find "123", if so, str1 will directly point to the "123" in the constant pool

The new keyword will open up a memory in the heap memory to store the object, that is, the locations of str3 and str4 in the heap memory are different

2. StringBuffer and StringBuilder
public class TestMain {
     public static void main(String[] args) {
          StringBuilder sb = new StringBuilder("111");
          sb.append("222");
          sb.append("111");
          sb.append("111");
          System.out.println(sb.toString());
     }
 }

StringBuffer has the same principle as StringBuilder. It maintains a char array at the bottom layer, and puts characters into the char array every time it appends. The difference is that StringBuffer is thread-safe, it synchronizes all methods, StringBuilder is thread-unsafe

In the above code, when only one StringBuilder object and the final toString() are generated, the new String() method will be called to convert the contents of the char array into String objects.

Disadvantages:
When the char array is not enough, it needs to be expanded, and the expansion needs to be copied, which reduces the efficiency to a certain extent.

Optimization point:
If you have an estimate of the length of the string to be spliced, you can specify the length in the constructor

Applicable scene:
If you want to manipulate a small amount of data, use String
Operate large amounts of data under multi-threaded string buffer operation StringBuffer
Operate large amounts of data in a single-threaded string buffer StringBuilder
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.