Java notes: Auto-boxing

Source: Internet
Author: User

One, type encapsulation device

For performance reasons, Java does not discard basic data types, which can be an unacceptable overhead if they are always used in the form of objects. Although the basic types provide the benefits of performance, they sometimes require representations of objects. For example, the base type cannot be passed as a reference, and the standard data structure of the Java implementation can only manipulate objects. In order to handle these cases, Java provides a type wrapper.

Second, automatic packing

The process of encapsulating numeric values into an object is called unpacking, and the process of extracting values from the type wrapper is called unpacking. Java already supports automatic boxing and automatic unpacking. When you need to convert a base type to an object, automatic boxing will inevitably occur. When you need to convert an object to a base type, automatic unpacking will inevitably occur. You should reasonably reduce the use of type wrappers when writing code to avoid unnecessary performance overhead.

classSolution {Static intunbox (Integer i) {returnI//Automatic Unpacking    }    StaticInteger Box (inti) {returnI//Automatic Boxing    }     Public Static voidMain (string[] args) {intA = unbox (100);//Automatic BoxingInteger b = Box (500); }}

Java notes: Auto-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.