1. Basic types of data

Buffer pool

New Integer(123) creates an object each time;

ValueOf (123) uses an object from the cache pool, and multiple calls will obtain a reference to the same object.

The simple implementation of the valueOf() method is to determine whether the value is in the cache pool and, if so, return the contents of the cache pool.

public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}
Copy the code

The compiler calls the valueOf() method during autoboxing, so multiple Integer instances with the same value and values in the cache pool are created using autoboxing, and the same object is referred to.

The buffer pools for basic types are as follows:

boolean values true and false

all byte values

short values between -128 and 127

int values between -128 and 127

char in the range \u0000 to \u007F

When using the wrapper type corresponding to these basic types, objects in the buffer pool can be used directly if the value is within the buffer pool range.

Of all the numeric buffer pools in JDK 1.8, IntegerCache is special in that its lower bound is -128 and its upper bound is 127 by default, but the upper bound is adjustable. When the JVM is started, Through – XX: AutoBoxCacheMax = to specify the size of the buffer pool, this option will set the JVM is initialized, a Java lang. IntegerCache. High system properties, The IntegerCache initialization then reads the system properties to determine the upper bound.

Second, the String

In Java 8, strings internally use char arrays to store data.

public final class String
    implements java.io.Serializable.Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];
}
Copy the code

After Java 9, implementations of the String class switched to byte arrays to store strings, while using a coder to identify which encoding was used.

public final class String
    implements java.io.Serializable.Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final byte[] value;

    /** The identifier of the encoding used to encode the bytes in {@code value}. */
    private final byte coder;
}
Copy the code

Java9 changes: space saving; The coder field has two default values: 0 for Latin-1 and 1 for UTF-16. The value of the coder field is used to calculate the length of the string. The value of the coder field is used to calculate the length of the string.

The value array is declared final, which means that the value array cannot refer to another array after it is initialized. And there is no internal method to change the value array, so String is guaranteed immutable.

Immutable benefits

You can cache hash values

String Pool required

security

Thread safety

String, StringBuffer and StringBuilder

String is immutable and therefore thread-safe

StringBuilder variable, not thread safe

The StringBuffer is variable, thread-safe, and synchronized internally

String Pool

The new String() method creates two different strings

Get the same string reference through the s1.intern() and s2.intern() methods

If you create a String as a literal “BBB”, the String is automatically placed in the String Pool.

Prior to Java 7, String pools were placed in run-time constant pools, which belong to the permanent generation. In Java 7, the String Pool is moved to the heap. This is because there is limited space for permanent generations, which can result in An OutOfMemoryError in scenarios where a large number of strings are used.