Home > Java > Javagetting Started > body text

In-depth understanding of automatic boxing and unboxing in java

王林
Release: 2019-12-19 11:55:29
forward
2898 people have browsed it

In-depth understanding of automatic boxing and unboxing in java

1. What is boxing and what is unboxing

Boxing: Convert basic data types into packaging classes.

Unboxing: Convert the wrapper class into a basic data type.

The packaging class corresponding to the basic data type:

int (a few bytes 4) - Integer

byte (1) - Byte

short (2) - Short

long (8) - Long

float (4) - Float

double (8) - Double

char (2 ) - Character

boolean (undefined) - Boolean

Free online video learning tutorial recommendation: java video tutorial

2. First Let’s take a look at manual boxing and manual unboxing

Example: Take int and Integer as examples

Integer i1=Integer.valueOf(3);
int i2=i1.intValue();
Copy after login
Copy after login

Manual boxing is done through valueOf. Everyone knows that = assigns the right value to On the left, 3 is of type int. When assigned to the left, it becomes an Integer wrapper class.

Manual unboxing is done through intValue(). You can see through the code that i1 changes from Integer to int

3. After reading it manually, let’s look at the automatic

In order to ease the work of technical personnel, Java has changed from jdk1.5 to automatic boxing and unboxing. Take the above example:

Manual:

Integer i1=Integer.valueOf(3);
int i2=i1.intValue();
Copy after login
Copy after login

Automatic

Integer i1=3;
int i2=i1;
Copy after login

This is automatically installed and disassembled by default.

4. Deepen your understanding of automatic boxing and unboxing from several questions

(1)

Integer a = 100;
int b = 100;
System.out.println(a==b);结果为 true
Copy after login

Reason: a will automatically Unboxing and comparing with b, so it is true

(2)

Integer a = 100;
Integer b = 100;
System.out.println(a==b);//结果为true
Integer a = 200;
Integer b = 200;
System.out.println(a==b);//结果为false
Copy after login

An interesting thing happened, why are the two variables the same, only the one with different values ​​is true , one is false.

Reason: In this case, we need to talk about the comparison symbol ==. The memory address of == comparison is the memory address of the object that comes out of new. When you see this, you may ask that there seems to be no new. Ah, but actually Integer a=200; 200 is preceded by new Integer by default, and the memory address used is different == The comparison is false, but why is 100 true? This is because of the constant pool in java. We can click on the source code of Integer to take a look.

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

When comparing between -128 and 127, the new object will not be new, but will be obtained directly from the constant pool, so 100 is true, 200 exceeds this range and then a new operation is performed. , so the memory addresses are different.

(3)

Integer a = new Integer(100);
Integer b = 100;
System.out.println(a==b);
//结果为false
Copy after login

This is similar to the 100 above. Take it from the constant pool. Why is it false?

Reason: The reason for new Integer(100). Although 100 can be taken from the constant pool, you cannot directly give new an object. The two memory addresses used are different.

(4)

Integer a = 100;
Integer b= 100;
System.out.println(a == b);
//结果true
a = 200;
b = 200;
System.out.println(c == d);
//结果为false
Copy after login

Reason: The value on the right side of = sign is assigned to a on the left, b is already a packaging class, 200 is not in the constant pool, assign the int type 200 to the packaging class, automatically Because the boxing is not in the constant pool, the object is new by default, so the result is false.

For more related articles and tutorials, you can visit: Java language introduction

The above is the detailed content of In-depth understanding of automatic boxing and unboxing in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template