Java based (ii) basic types of data types, packaging and automatic entry box

We know the basic data types byte, short, int, long, float, double, char, boolean, corresponding wrapper classes are Byte, Short, Integer, Long, Float, Double, Character, Boolean. Basic data on the type of presentation refer to Java base (a) eight basic data types

So why the need packaging?

JAVA is an object-oriented language, a lot of class and method parameters are required to use the object, but the basic data types is not object-oriented, which caused a lot of inconvenience.

Such as: List<int> = new ArrayList<>();it can not be compiled by

To solve this problem, we introduce a packaging, by definition, it is the fundamental type " wrap up ", so that objects have properties, including the properties and methods can be added, located in the java.lang package.

Packing and unpacking

Since there is the basic data types and packaging, there is bound to the transition between them, such as:

public static void main(String[] args) {
    Integer a = 0;
    for(int i = 0; i < 100; i++){
        a += i;
    }
}

The basic data types into packaging process is called " packing ";

The basic data types into packaging process called " unboxing ";

Automatic and automatic packing unpacking

Java for easy unpacking and packing operation, automatically provides the functionality of the entry box, which greatly facilitates the programmers. So in the end is how to achieve it?

The above example is an automatic packing and unpacking process, we obtain by decompiling tool,

public static void main(String[] args) {
    Integer a = Integer.valueOf(0);
    for (int i = 0; i < 100; i++) {
        a = Integer.valueOf(a.intValue() + i);
    }
}

We can easily find, call the main two methods, Integer.intValue () and Integer.valueOf (int i) method

View Integer source, we find the corresponding codes:

/**
 * Returns the value of this {@code Integer} as an
 * {@code int}.
 */
public int intValue() {
    return value;
}
/**
 * Returns an {@code Integer} instance representing the specified
 * {@code int} value.  If a new {@code Integer} instance is not
 * required, this method should generally be used in preference to
 * the constructor {@link #Integer(int)}, as this method is likely
 * to yield significantly better space and time performance by
 * caching frequently requested values.
 *
 * This method will always cache values in the range -128 to 127,
 * inclusive, and may cache other values outside of this range.
 *
 * @param  i an {@code int} value.
 * @return an {@code Integer} instance representing {@code i}.
 * @since  1.5
 */
public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}

Obviously, we have come to us, Java to help you hide the internal details.

Unpacking process is by Integer entity calling intValue () method;

The packing process is called Integer.valueOf (int i) method, you help direct a new Integer object

So what place will be automatically enables me?

it's actually really easy

1. When added to the collection, automated packing

2. relates to the operation time, "add, subtract, multiply, divide," and "Comparative equals, compareTo", automatic unpacking

Note basis point

In the above code, on Integer valueOf (int i) method has IntegerCache class, there is a condition determination process in autoboxing

if (i >= IntegerCache.low && i <= IntegerCache.high)

Combined with comments

This method will always cache values in the range -128 to 127,

inclusive, and may cache other values outside of this range.

The effect that: the method always cached values ​​between -128 and 127, while for values ​​outside of this range are also possible cache.

So why might the cache? In fact, IntegerCache source code can get the answer

String integerCacheHighPropValue =
    sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
    try {
        int i = parseInt(integerCacheHighPropValue);
        i = Math.max(i, 127);
        // Maximum array size is Integer.MAX_VALUE
        h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
    } catch( NumberFormatException nfe) {
        // If the property cannot be parsed into an int, ignore it.
    }
}

Because the maximum value of the cache is configurable. This design has some advantages, we have the flexibility to adjust to improve the performance of the actual situation of the application.

Similarly there are:

Byte and ByteCache, buffer value range of -128 to 127, the configuration is not fixed

Short and ShortCache, buffer value range of -128 to 127, the configuration is not fixed

Long and LongCache, buffer value range of -128 to 127, the configuration is not fixed

Character and CharacterCache, buffer range from 0 to 127, the configuration is not fixed

Guess you like

Origin blog.csdn.net/fengdongsuixin/article/details/92800533