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();
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();
Automatic
Integer i1=3; int i2=i1;
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
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
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; }
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
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor