Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

StringBuilder is a more commonly used class than String. In terms of seniority, StringBuilder comes later and is not available until JDK1.5. Note that another commonly used string class, StringBuffer, is available in JDK1.0.

So why is this word much longer than String the most common?

Here’s why:

  1. StringBuilderTo be able to achieveMutable character sequences;
  2. in.append()When compared toString+ =Save a lot of space and time resources;
  3. Regardless of thread safety, thanStringBuffer More efficient.

⭐️ Talk less, show u the code


/** * Tests the performance difference between String and StringBuilder */

public class StringTrip {
    public static void main(String[] args) {

        String strString  = "";
        // Record an initial value
        long num_start = Runtime.getRuntime().freeMemory(); // Obtain the remaining system memory space
        long time_start = System.currentTimeMillis(); // Get the current system time
        stringAdd(strString);             // Add with String
        long num_end = Runtime.getRuntime().freeMemory();
        long time_end = System.currentTimeMillis();
        // Use the initial free memory space - the free memory space after adding characters = the size of memory used
        // Then run the program with the end of the timestamp - the start of the timestamp = the time to add characters in between
        System.out.println("String memory occupied:"+ (num_start-num_end));
        System.out.println("String occupied time:"+ (time_end-time_start));

        StringBuilder strBuilder = new StringBuilder();
        long num_start1 = Runtime.getRuntime().freeMemory(); // Obtain the remaining system memory space
        long time_start1 = System.currentTimeMillis(); // Get the current system time
        stringBuilderAdd(strBuilder);    // Use StringBuilder to add
        long num_end1 = Runtime.getRuntime().freeMemory();
        long time_end1 = System.currentTimeMillis();
        System.out.println("StringBuilder occupies memory:"+ (num_start1-num_end1));
        System.out.println("StringBuilder takes time:"+ (time_end1-time_start1));
    }

    static void stringAdd(String s) {  // Add with String

        for (int i=0; i< 5000; i++) {
            s = s + i; // Generate 5000 objects}}static void stringBuilderAdd(StringBuilder s) { // Use StringBuilder to add
        for (int i = 0; i< 5000; i++) { s.append(i); }}}Copy the code

Here is the result:

  • Memory occupied: one is 22624024, the other is 0;
  • Occupied time: one is 99 and the other is 1.

It’s like one in the sky and one in the ground.

⭐ ️ summary

This lesson is gao Qi teacher request hand knock, very important!!

In addition to remembering that String can create a large number of useless memory objects and cause memory leaks, learn how to do simple performance tests with Runtime.geTruntime ().freememory () and System.CurrentTimemillis ().

If you have more practical analysis experience, please feel free to comment ~ ❤