Java Auto-Boxing automatic unpacking

Source: Internet
Author: User

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:

Java basic data Types
INDEX Basic type Size Range of values Default value Package 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 definition

The automatic boxing and unpacking mechanism is introduced in Java 1.5:

(1) Auto-boxing : The basic types are packaged with their corresponding reference types so that they have the attributes of the object and can be called by ToString (), Hashcode (), GetClass (), Equals (), and so on.

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) Auto-Unpacking : Objects of reference types such as Integer and double are re-simplified to the basic type of data, in contrast to the auto-boxing direction.

As follows:

int New Integer (2); // this is unpacking .

The compiler internally calls int intvalue () to return the int value of the integer object

Note: Automatic boxing and unpacking is done by the compiler, and the compiler determines whether boxing and unboxing are performed at compile time based on syntax.

3. Differences between basic data types and objects

    • The base data type is not an object , which is a variable, constant, defined using an int, double, boolean, and so on.
    • There is no callable method for the base data type .
eg:  int t = 1;     T.   =1; t.  There are a number of ways that you can invoke it later.

4. When to automatically pack

int/ / unboxing, actually executed int t = I.intvalue ();  

It is also possible to disassemble the container while the operation is in progress.

Integer i = ten; System.out.println (i+ +);

5. When to automatically pack

// The number outside the -128~127 Integer i1 =200;    =200;           System.out.println ("I1==I2:" + (i1==i2));                     // The number within the -128~127 Integer i3 =100;    =100;   System.out.println ("I3==I4:" + (I3==I4));
Results of the output
false i3 true

Description :

Equals () compares the values (contents) of two objects.

"= =" compares the two-object reference (memory address) with the same, and is used to compare the values of variables of two base data types for equality.

As mentioned earlier, the automatic boxing of int is an Integer that is executed by the system. valueOf (int i), first look at the source code of Integer.java:

 Public Static Integer valueOf (int  i) {    if(i >= -128 && i <= integercache.high) //if not set, ingegercache.high default is 127        return integercache.cache[i + +];     Else        return New Integer (i);}

For values between –128 to 127 (by default, 127) , integer.valueof (int i) returns a cached Integer object (not a new object)

So in the example, i3 and I4 actually point to the same object.

In other values , the execution integer.valueof (int i) returns a new integer object , so in the example, I1 and I2 point to a different object.

Of course, when the auto-boxing function is not used, as in the case of ordinary class objects, consider the following example:

Integer i3 =new integer (i4 =new integer (System.out.println ("I3==i4 : "+ (I3==I4)); // Show False

Thanks: Thank you for your patience and reading!

Java Auto-Boxing automatic unpacking

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.