Java Automatic unpacking and boxing (basic data types and reference types)

Source: Internet
Author: User

(This article is not an original article, excerpt, forget the source, sorry)

1.Java Data Types before we introduce the automatic boxing and unpacking of Java, let's look at the basic data types of java. in Java, data types can be divided into two major types, Primitive type (base type) and reference type (reference type). The value of the base type is not an object, and methods such as ToString (), Hashcode (), GetClass (), Equals () of the object cannot be called. So Java provides a wrapper type for each of the basic types. As follows:    
index basic type size numeric range default value wrapper type
1 Boolean --- True,false False Boolean
2 Byte 8bit -2^7--2^7-1 0 Byte
3 Char 16bit \u0000-\UFFFF \u0000 Character
4 Short 16bit -2^15--2^15-1 0 Short
5 Int 32bit -2^31--2^31-1 0 Integer
6 Long 64bit -2^63--2^63-1 0 Long
7 Float 32bit IEEE 754 0.0f Float
8 Double 64bit IEEE 754 0.0d Double
9 void --- --- --- Void
2.Java Automatic packing and unpacking definitionthe automatic boxing and unpacking mechanism is introduced in Java 1.5: (1) Auto-boxing: The basic types are wrapped with their corresponding reference types, so that they have the characteristics of the object, you can call ToString (), Hashcode (), GetClass (), Equals () and other methods. as follows:Integer a=3; //This is auto-boxing in fact, the compiler calls the static integer valueOf (int i) method, valueOf (int i) returns an Integer object representing the specified int value, then it becomes this:Integer a=3; = = Integer a=integer.valueof (3); (2) Unpacking:In contrast to automatic boxing, objects of reference types such as integers and double are re-simplified to the basic type of data. as follows:int i = new Integer (2); //This is unpacking

Java Automatic unpacking and boxing (basic data types and reference types)

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.