Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the principle of automatic packing and unpacking in Java

2025-04-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to analyze the principle of automatic packing and automatic unpacking in Java, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

Data types in Java can be divided into two types, one is Primitive Type (basic type) and the other is Reference Type (reference type). The numeric value of the basic type is not an Object object, and you cannot call the object's toString (), getClass (), and so on. Java provides wrapper types for each basic type, as shown in the following figure:

Automatic boxing (autoboxing) and unpacking (autounboxing) of Java basic data (Primitive) types have been provided since J2SE 5.0. The Java language specification mentions that in many cases automatic boxing and automatic unpacking are done by the compiler itself. In this case, the packaging becomes a box, and unpacking is called unpacking.

The eight basic data types of autoboxing Java will automatically become corresponding wrapper types under certain conditions. For example, the Integer (int) type is as follows:

@ Test

Public void boxing () {

Integer i1 = 10

Integer i2 = 10

Integer i3 = 128

Integer i4 = 128

System.out.println ("i1==i2:" + (i1==i2))

System.out.println ("i3==i4:" + (i3==i4))

System.out.println ("i1.equals (i2):" + i1.equals (i2))

System.out.println ("i3.equals (i4):" + i3.equals (i4))

}

The output is shown in the figure:

1. When comparing wrapper types, i3 calls Integer.valueOf () to automatically boxing the basic data type as wrapper type. The source code is as follows:

Integer objects automatically cache int values in the range of low~high (- 128 to 127), which are automatically boxed into wrapper classes if they are beyond this range.

In the equals method implemented by the 2.Integer wrapper class, as long as the current object being compared is an Integer instance, it will be automatically unboxed to the basic data type. The source code of the equals method of the Integer class is as follows:

Other types are implemented as follows:

1) the wrapper classes Integer, Short, Byte, Character, Long, such as valueOf () or intValue (), implement similar methods.

2) the implementation of valueOf () or intValue () methods of Double and Float is similar.

3) the implementation of valueOf () or intValue () method of Boolean is a trinomial operation, such as return (b? TRUE: FALSE)

Automatic unpacking Java eight wrapper types will automatically become corresponding basic data types when used under certain conditions. For example, int (Integer) types are as follows:

@ Test

Public void boxing () {

Integer i1 = 10

Int i2 = 10

Int i3 = 128

Integer i4 = 128

System.out.println ("i1==i2:" + (i1==i2))

System.out.println ("i3==i4:" + (i3==i4))

}

The output is shown in the figure:

When the program is executed, i4 will call the Integer.intValue () method to automatically unpack the wrapper. The type of the wrapper is the basic data type. The source code is as follows:

When the wrapper type is compared with the basic data type, the wrapper type is automatically unboxed to the basic data type.

Source code reference: http://blog.yoodb.com/yoodb/article/detail/1085, Wechat "reference" function, will lead to java encoding format problems, it is recommended to manually type the code or source code to refer to this path.

Note: equals () compares whether the values (contents) of the two objects are the same, while "=" compares whether the references (memory addresses) of the two objects are the same, and is also used to compare whether the values of the variables of the two basic data types (int) are equal. " When both operands of the = = "operator are references to the wrapper type, compare whether they point to the same object, and if one of the operands is an expression (that is, an arithmetic operation), it compares the numeric value (that is, it triggers the automatic unboxing process). For the wrapper type, the equals method does not perform type conversion.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report