Brief introduction to Java automatic disassembly box

In the interview process, when interviewers ask basic questions, they often ask about the disassembly and assembly box of Java. In fact, this question is not very difficult, but if they don't pay attention to self-study at ordinary times, they may be confused. Therefore, the author summarizes this question and promotes it together!

1、 Disassembly and assembly box concept

The so-called disassembly box is from jdk1 After 5, the mutual conversion between java basic types and reference types.

1.1 unpacking

Unpacking is the action of converting long, integer, double, float and other reference types corresponding to the capitalization of the initial letter of the basic data type into the basic data type, which is called unpacking.

1.2 packing

Boxing means that the basic data types of Java, such as byte, int, short, long, double, float, Boolean and char, are not declared as corresponding reference types when defining data types. The action of automatically converting them into reference types under the processing of the compiler is called boxing.

2、 Related applications of disassembly and assembly box

At jdk1 After 5, it will be convenient for us to convert basic types and reference types:

The above describes some basic points and usage of the disassembly and assembly box, but there are still some points to pay attention to when using the disassembly and assembly box. These points are summarized below.

3、 Attention

First, paste a code as follows:

Briefly explain:

The first result is false because different objects are created, so they are different;

But why are the results of the second and third different?

The source code of integer class is posted below to analyze this problem from the perspective of source code:

The above code means that there is a range for automatic box disassembly. Once it exceeds this range, it will not point to the same object, but return a newly created object. This range can be reflected in an internal private class integercache in the integer class. The source code is as follows:

From here, we can see that the range value is between [- 128127].

Note that the implementation of valueof methods of integer, short, byte, character and long is similar. The implementation of the valueof method of double and float is similar.

Summary: the scope of the basic types of these automatic disassembly boxes is as follows:

1. Value of boolean type

2. All byte values

3. Value of short type in - 128 ~ 127

4. Int type values from - 128 to 127

5. Char type value between \ u0000 ~ \ u00ff

Double and float are different. Let's take double as an example and post the code for discussion:

Note why the output of the above code is false? Similarly, we still discuss it with the valueof method in the double class, and it will be clear at a glance after posting the source code:

In other words, no matter what range of value your double is, it returns you a new object. Float is the same as double, but I won't repeat it.

The above is the author's sorting of the disassembly box. If readers have different views, they can put forward them in the comment area, and the author will modify them again!

I hope it will help you in your study, and I also hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>