Java automatic packing automatic unpacking and java packing

Source: Internet
Author: User

Java automatic packing automatic unpacking and java packing
 1. Java Data Type

Before introducing Java's automatic packing and unpacking, let's take a look at the basic data types of Java.

In Java, there are two types of data: Primitive Type and Reference Type ). The value of the basic type is not an object. You cannot call methods such as toString (), hashCode (), getClass (), and equals () of the object. Therefore, Java provides packaging types for each basic type. As follows:

 

 

Java Basic Data Type
INDEX Basic Type Size Value Range Default Value Packaging 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 1, 754 0.0f Float
8 Double 64bit IEEE 1, 754 0.0d Double
9 Void --- --- --- Void

2. Java automatic packing and unpacking Definition

Java 1.5 introduces the automatic packing and unpacking mechanism:

(1)Automatic Packing: Wrap the basic types with their corresponding reference types so that they have object characteristics. You can call methods such as toString (), hashCode (), getClass (), and equals.

As follows:

Integer a = 3; // This is automatically packed

In fact, the compiler calls the static Integer valueOf (int I) method. valueOf (int I) returns an Integer object that represents the specified int value., Then it becomes like this:

Integer a=3;   =>    Integer a=Integer.valueOf(3);

 

(2)Automatic unpacking: In the opposite direction of automatic packing, the reference type objects such as Integer and Double are re-simplified to basic type data.

As follows:

Int I = new Integer (2); // This is the Unbox

The compiler calls int intValue () internally to return the int value of the Integer object.

Note: automatic packing and unpacking are completed by the compiler. during compilation, the compiler determines whether to perform packing and unpacking Based on the syntax.

 

3. Differences between basic data types and objects

  • The basic data type is not an object.That is, variables and constants defined by int, double, and boolean are used.
  • No callable methods for basic data types.
Eg: int t = 1; t. There is no method behind it. Integer t = 1; t. There are many methods that you can call later.

 

4. When to automatically pack

Integer I = 10; // The int t = I; // In case of unboxing, int t = I. intValue () is actually executed ();

 

During calculation, you can also unpack the box.

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

 

5. When to automatically pack

// In-128 ~ Integer i1 = 127; Integer i2 = 200; System. out. println ("i1 = i2:" + (i1 = i2); // In-128 ~ Integer i3 = 127; Integer i4 = 100; System. out. println ("i3 = i4:" + (i3 = i4 ));
Output result
 i1==i2: false i3==i4: true

 

Description:

Equals () compares whether the values (content) of two objects are the same.

"=" Compares the two objects with the same reference (memory address) and compares the variable values of the two basic data types.

 

As mentioned above, the automatic int packing is performed 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 no value is set, IngegerCache. the default value of high is 127 return IntegerCache. cache [I + 128]; else return new Integer (I );}

For-Value range: 128 to 127 (default value: 127), Returned by Integer. valueOf (int I)YesInteger object (not a new object)

In this example, i3 and i4 actually point to the same object.

WhileOther values, Execute Integer. valueOf (int I)The returned result is a new Integer object.In this example, i1 and i2 point to different objects.

Of course, when the automatic packing function is not used, it is the same as a common Class Object. See the following example:

Integer i3 = new Integer (100); Integer i4 = new Integer (100); System. out. println ("i3 = i4:" + (i3 = i4); // display false

 

Thank you! Thank you for your patience!

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.