• String String constant
  • StringBuffer String variable (thread-safe)
  • StringBuilder String variable (non-thread-safe)

Running speed StringBuilder > StringBuffer > String


String String constants (result of JVM optimization)

Misconception :String can be modified

String z = "Introduction";
z = z + "Stop";

Copy the code

why

Final, immutable

 private final char value[];
Copy the code

The JVM processes this code like this: it first creates a Z object, assigns “entry”, then creates another Z object, assigns “station” on the second line of code, and then garbage collects the first Z object.


StringBuffer String variable (thread-safe)

Array.copyof () : char[] : char[] : char[] : char[] : char[] : char[] , of course, this affects performance, so you can customize the size of an object as needed when you create it

No final modifier, mutable, essentially an array of characters

abstract class AbstractStringBuilder implements Appendable.CharSequence {
    /** * The value is used for character storage. */
    char[] value;
    / /...
}
Copy the code

Synchronized modifier, thread-safe

public synchronized StringBuffer append(Object obj) {
        toStringCache = null;
        super.append(String.valueOf(obj));
        return this;
}
Copy the code

StringBuilder String variable (non-thread-safe)

Array.copyof () : char[] : char[] : char[] : char[] : char[] : char[] , of course, this affects performance, so you can customize the size of an object as needed when you create it.

A string variable, essentially an array of characters

abstract class AbstractStringBuilder implements Appendable.CharSequence {
    /** * The value is used for character storage. */
    char[] value;
    / /...
}
Copy the code

The thread is unsafe and not locked

 @Override
public StringBuilder append(CharSequence s) {
    super.append(s);
    return this;
}
Copy the code

String Benefits of a String that cannot be modified

  1. String pooling is only possible if strings are immutable. The implementation of a string pool can save a lot of heap space at run time because different string variables point to the same string in the pool. But if strings are mutable, String interning will not be implemented. Because then, if a variable changes its value, the values of other variables that refer to that value change too.
  2. If the string is mutable, this can cause serious security problems. For example, the database user name and password are passed as strings to get a connection to the database, or in socket programming, the host name and port are passed as strings. Because a string is immutable, its value is immutable, or hackers can exploit a loophole by changing the value of the object to which the string points.
  3. Because strings are immutable, they are multithread safe, and the same string instance can be shared by multiple threads. This eliminates the need to use synchronization for thread-safety reasons. Strings themselves are thread-safe.
  4. Class loaders use strings, and immutability provides security so that the correct class is loaded. For example, if you want to load the java.sql.Connection class, and the value is changed to myhacked.Connection, you will do some unknown damage to your database.
  5. Because the string is immutable, hashCode is cached when it is created and does not need to be recalculated. This makes strings suitable as keys in maps, and strings are processed faster than other key objects. This is why keys in a HashMap tend to use strings.

conclusion

  1. StringBuilder is used in single-threaded environments and StringBuffer is used in multi-threaded environments
  2. Use StringBuilder or StringBuffer to avoid “+” concatenation (in loops, a String is created every time a “+” is executed, so there is a lot of object creation and recycling overhead)

Original text: rumenz.com/rumenbiji/J…