Java Unpacking and Boxing

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.