Java Fundamentals (35): Boxing and unpacking---conversions between basic types and wrapper classes in Java

Source: Internet
Author: User

The basic types and wrapper classes often need to be converted to each other, in the case of an Integer (the operation of several other packaging classes is similar):

With the introduction of the automatic boxing and unpacking mechanism in JDK1.5 , the conversion between the wrapper class and the base type is easier and more convenient.

So what is packing and unpacking? Let's look at each other.

Boxing: converts the basic type into a wrapper class , which has the properties of the object and can be divided into manual boxing and automatic packing .

unpacking: In contrast to boxing, the wrapper class object is converted to a basic type of value, and can be divided into manual unpacking and automatic unpacking

The following code example illustrates the process of boxing and unpacking:

The result is:

1  Public classHelloWorld {2      Public Static voidMain (string[] args) {3         4         //defining a double type variable5         DoubleA = 91.5;6         7          //Manual Boxing8Double B =NewDouble (a); 9         Ten         //Automatic Boxing OneDouble C =A;  A  -SYSTEM.OUT.PRINTLN ("Boxed result is:" + B + "and" +c); -          the         //defines a double wrapper class object with a value of 8 -Double d =NewDouble (87.0); -          -         //Manual Unpacking +         DoubleE =D.doublevalue (); -          +         //Automatic Unpacking A         Doublef =D; at          -SYSTEM.OUT.PRINTLN ("The result after unpacking is:" + E + "and" +f); -     } -}

Java Fundamentals (35): Boxing and unpacking---conversions between basic types and wrapper classes in Java

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: 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.