Table of Contents
1. Preface
2. Packaging class
3. Automatic boxing and automatic unboxing
4. Interger cache
5. Answer the question
Home Java javaTutorial How to use Java automatic boxing, automatic unboxing and Integer caching

How to use Java automatic boxing, automatic unboxing and Integer caching

Apr 24, 2023 pm 10:28 PM
java integer

1. Preface

What are automatic boxing and automatic unboxing? What is Integer cache? What's the relationship between them?

Let’s look at a question first.

Integer a = new Integer(1);
Integer b = new Integer(1);
System.out.println(a==b);
Integer c = 1;
Integer d = 1;
System.out.println(c==d);
Integer e = 128;
Integer f = 128;
System.out.println(e==f);
Copy after login
Copy after login

Answer first, look at the answer later.

The answer is false true false, are you correct?

Now that one piece has appeared, let’s share the knowledge points together

2. Packaging class

There are eight basic data types in Java, which can be divided into three categories:

  • Character type: char

  • Boolean type: boolean

  • Numeric type: byte short int long float double

Packaging classes wrap eight basic data types into classes so that they can use Java's three major features: encapsulation, inheritance, and polymorphism. The corresponding relationship is as follows:

Basic data typeCorresponding packaging class
byteByte
shortShort
intInteger
#longLong
float#Float
doubleDouble
booleanBoolean
charCharacter

The six packaging classes corresponding to numerical types all inherit from the Number class.

3. Automatic boxing and automatic unboxing

The eight basic data types correspond to the eight packaging classes, so how do they perform data conversion?

//基本数据类型转包装类
//1.有参构造
Integer a = new Integer(1);
//2.实际上,有参构造的参数也可以是字符串,不过要使用正确的数据,“123abc”不可能会转换为Integer类型
Integer b = new Integer("123");
//3.valueOf()
Integer c = Integer.valueOf(123);
//包装类转基本数据类型(xxxValue()  float是floatValue() double是doubleValue())
int d = a.intValue();
Copy after login

The above forms are relatively consistent with cognition. To obtain an object, you can use new or call a certain method. To obtain a value, call a certain attribute of the object.

After Java 5.0, you don’t have to be so troublesome. New features of automatic boxing and automatic unboxing have been added. In fact, the two concepts are very easy to understand.

int a = 10;
Integer b = a;  //自动装箱
int c = b;  //自动拆箱
Copy after login

At first glance, the form of object=numeric value does not conform to cognition, but it can be achieved with the help of automatic boxing and automatic unboxing. In fact, the compiler still implements it with the help of valueOf() and xxxValue().

Let’s take a look at the valueOf() source code.

/**
     * 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);
}
Copy after login

valueOf() does not simply return an Integer object, but first makes a judgment. If the input data matches a certain range, it will return a specific object. From the comments, this range The default is [-128,127], and may be a larger range; beyond this range, a new object will be returned. The IntegerCache data used is the cache of Integer.

4. Interger cache

Numerical calculations are used frequently in daily life. If you keep getting new Integer objects, the overhead will be very large. Therefore, Java will automatically generate a new Integer object when executing the program. Static arrays are used as caches. The default cache array range corresponding to Integer is [-128,127]. As long as the data is within this range, the corresponding object can be obtained from the cache.

Look at the IntegerCache source code.

/**
     * Cache to support the object identity semantics of autoboxing for values between
     * -128 and 127 (inclusive) as required by JLS.
     *
     * The cache is initialized on first usage.  The size of the cache
     * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
     * During VM initialization, java.lang.Integer.IntegerCache.high property
     * may be set and saved in the private system properties in the
     * sun.misc.VM class.
     */
private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static final Integer cache[];
    static {
        // high value may be configured by property
        int h = 127;
        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.
            }
        }
        high = h;
        cache = new Integer[(high - low) + 1];
        int j = low;
        for(int k = 0; k < cache.length; k++)
            cache[k] = new Integer(j++);
        // range [-128, 127] must be interned (JLS7 5.1.7)
        assert IntegerCache.high >= 127;
    }
    private IntegerCache() {}
}
Copy after login

As you can see, IntegerCache is a static internal class of Integer. IntegerCache.cache called by valueOf() is an array object. The size of the array depends on the maximum and minimum values ​​in the range. The default is [- 128, 127], of course (the comment says) this range can also be modified through the JVM (I don't understand this). Then each element in the array will be assigned an Integer object, and the cache will be formed.

There is an array cache, which means that if the value is in [-128,127], the Integer object created using valueOf() or automatic boxing is taken out from the array, so the memory address pointed to by the object is exactly the same. If you use new or exceed this range, the object must be re-created.

Of course, not only Integer has a caching mechanism, Byte, Short, Long, and Character all have caching mechanisms. The range of Byte, Short, Integer and Long is -128 to 127, and the range of Character is 0 to 127.

5. Answer the question

Integer a = new Integer(1);
Integer b = new Integer(1);
System.out.println(a==b);
Integer c = 1;
Integer d = 1;
System.out.println(c==d);
Integer e = 128;
Integer f = 128;
System.out.println(e==f);
Copy after login
Copy after login

1. Even if the two objects created by new have the same value, they point to different memory addresses. Using == for comparison returns a false

2. Automatic boxing and caching mechanism. The two objects are actually the same and the return result is true

3. Beyond the cache range, a new object will be new during execution. If the two objects are different, return The result is false

The above is the detailed content of How to use Java automatic boxing, automatic unboxing and Integer caching. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

How to Run Your First Spring Boot Application in Spring Tool Suite? How to Run Your First Spring Boot Application in Spring Tool Suite? Feb 07, 2025 pm 12:11 PM

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo

See all articles