1. Automatic box packing and automatic unpacking
-Refers to the conversion of the original type and the package type to each other.
2. Why should I have a packing and unpacking mechanism?
-Make Java code more thorough to object-oriented
-Support from jdk1.5 onwards, make the code more concise, elegant, easy to understand.
4. What problems are solved by packing and unpacking?
-Because the collection of Java can only be stored in objects, does not support the original type, in the jdk1.5 need to manually convert the original type and package type, jdk1.5 after the introduction of boxing and unpacking can be automatically converted, so that the code easier to more concise, convenient, easy to understand.
5. Packing and unpacking implementation process:
/** * boxing, unpacking * * @author Szy * */ public class Encapsulationtype { public static Span style= "COLOR: #0000ff" >void main (string[] args) {Integer I = 10; Span style= "COLOR: #008000" >// boxing int n = i; // unpacking }}
使用javap -c 的命令反编译后我们发现:装箱时用了 Integer.valueOf(), 拆箱用了Integer.intValue()
6.值传递
- 原始类型值传递,是将值本身传递过去且原始类型保存在栈中。
- 引用类型传递的时候是将引用传递过去,程序根据这个引用去堆中获取相应的值。引用类型的值保存在堆中,引用变量保存在栈中。
Java Unpacking and Boxing