A deep analysis of Java automatic box unboxing _java

Source: Internet
Author: User
This is jdk1.5 after the introduction of the new content, as the best memory of publishing, resolutely decided to use a blog instead of my memory:
The Java language Specification says: In many cases packaging and unpacking are done by the compiler itself (in which case the packaging becomes boxed and the solution package is called the unboxing);
In fact, according to my own understanding, automatic boxing can be easily understood to encapsulate basic data types as object types to conform to Java object-oriented objects, such as int:

Copy Code code as follows:

Declare an Integer object
Integer num = 10;
The above statement is the use of automatic boxing: resolved to
Integer num = new integer (10); The above is a good embodiment, because 10 is a basic data type, in principle it is not directly assigned to an object Integer, but after jdk1.5 you can make such a statement, this is the charm of automatic boxing
Automatically converts the base data type to the corresponding package type. After becoming an object, you can invoke all the methods declared by the object
Automatic unboxing: So name incredible is the conversion of an object to a basic data type:
Packing
Integer num = 10;
Split Box
int num1 = num; A typical use of an automatic unboxing is when an operation is performed: The object is not performed directly, but is converted to the basic data type before subtraction
Integer num = 10;
An automatic unboxing is implied in the calculation
System.out.print (num--); haha should feel very simple, I would like to say a little bit more difficult,
Number outside the -128~127
Integer NUM1 = 297; Integer num2 = 297;
System.out.println ("num1==num2:" + (num1==num2));
The number within the -128~127
Integer num3 = 97; Integer num4 = 97;
System.out.println ("NUM3==NUM4:" + (NUM3==NUM4)); The result of printing is: Num1==num2:false num3==num4:true

Strange: This is due to the Java for the integer and int automatic boxing and unboxing design, is a pattern: Call the mode (flyweight)
To increase reuse of simple numbers, the Java definition: when automatically boxed for values from –128 to 127, when they are boxed as integer objects, there is a reuse in memory and there is always only one object
And if you exceed the value from –128 to 127, the boxed integer object is not reused, which is equivalent to creating an integer object each time it is boxed. You got that?
The above phenomenon is caused by the use of automatic boxing, if you do not use automatic boxing, but with the general class, with new to instantiate, will be a new one every time the object;
This automatic box unboxing is applied not only in the basic data types, but also in the string class, such as when we often declare a string object:
Copy Code code as follows:

String str = "SL";
Instead of the following declaration method
String str = new String ("SL");

The basic data (primitive) type of automatic boxing (autoboxing), unboxing (unboxing) is the function that is provided from J2SE 5.0. While it is convenient for you to pack the basic data types, it is convenient to show that the details are hidden, and it is recommended that you use them to differentiate between the basic data types and the objects.
Autoboxing and unboxing
In Java, everything you want to work with is almost all objects (object), for example, the scanner used previously is an object, a string is also an object, and then more objects are seen. However, the basic (primitive) data type is not an object, that is, a variable that you define with int, double, Boolean, and literal constants that you write directly in.
The convenience of manipulating objects has been largely seen in the previous section, and people who have been using Java for some time know that it is sometimes necessary to convert basic data types to objects. For example, when you use the Map object to put () methods, the parameters that you need to pass in are objects rather than basic data types.
To wrap a basic data type as an object by using the package type (wrapper Types), you already know in the previous section that before J2SE 5.0, you would use the following statement to wrap an int as an integer object: integer integer = new Integer (10);
The automatic boxing feature is provided after J2SE 5.0, and you can package the base data type directly using the following statement: integer integer = 10;
When compiling, the compiler automatically determines whether an automatic boxing action is made according to the statement you write. The integer reference in the example above is an instance of the integer class. The same action can be applied to basic data types such as Boolean, Byte, short, char, long, float, double, respectively, using the corresponding package type (wrapper Types) Boolean, Byte, short, Character, Long, float, or double. Use the automatic Boxing feature directly below to overwrite example 4.4.

Example 4.5 Autoboxdemo.java
Copy Code code as follows:

public class Autoboxdemo {
public static void Main (string[] args) {
Integer data1 = 10;
Integer data2 = 20;
To double and divide by 3.
System.out.println (Data1.doublevalue ()/3);
Make a comparison of two values
System.out.println (Data1.compareto (data2));
}
}

The program seems to be a lot simpler, data1 and data2 in the runtime is an integer instance, you can directly to the object operation. The results are as follows:
3.3333333333333335
–1
The method of automatic boxing application can also be as follows:
Copy Code code as follows:

int i = 10;
Integer integer = i;

You can also use a more generalized Java.lang.Number class for automatic boxing. For example:
Number number = 3.14f;
The 3.14f is automatically boxed as float and assigned to number.
Starting from J2SE 5.0, automatic boxing can also be automatically disassembled (unboxing), which means that the basic data form information in the object is automatically removed from the object. For example, here's what you can say:
Copy Code code as follows:

Integer Foointeger = 10;
int fooprimitive = Foointeger;

Foointeger reference to an instance of automatic boxing to Integer, if assigned to a variable fooprimitive of type int, it automatically becomes an int type and then assigned to Fooprimitive. In the operation, can also be automatic boxing and unpacking. For example:
Copy Code code as follows:

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

The example above shows 20 and 10, the compiler will automatically boxing and unboxing, that is, 10 will be boxed first, and then at i + 10 o'clock will be first to remove the box, the addition operation; i++ the row is also the first to remove the box and then increment operations. Let's look at one more example:
Copy Code code as follows:

Boolean Boo = true;
System.out.println (boo && false);

The same boo used to be a Boolean instance, and when the and operation was performed, the Boo was first disassembled, then the and with false, and the result would appear false.
//////////////////////////////////////////////////////////////////
Boxing: Converting from base type to object type, called boxing, * * * unboxing: Converting from object to base type operation, called unpacking. This operation is much more used in the reflection process.
Boxing: Create an object instance in the heap to copy the value you specify into it; * * Unboxing: discriminant reference to the heap of information is to be broken into the type, is to remove the heap value to the stack variables, otherwise reported exception
///////////////////////////////////////////////////////////////////
Boxing is a hermit transformation of a value type to type object or to any interface type implemented by that value type.
Boxing a value type assigns an object instance and copies the value to the new object.
Copy Code code as follows:

int i=123;
Object o=i;

The result of this sentence is to create an object o on the stack that references the value of type int on the heap. The value is assigned to the variable i
A copy of the value type value of the
The following is a display to perform boxing conversions
Copy Code code as follows:

int i=123;
Ojbect o= (object) I;

This example converts the integer variable I to object o by boxing. In this way, the value stored in the variable i is changed from 123 to 456. This example shows that the object retains the original copy of the content. That is 123.
Unboxing is a display conversion from an object type to a value type or from an interface type to a value type that implements the interface. Unboxing operations include:
Examine the object instance to make sure it is a boxed value of the given value type.
Copies the value from an instance to a value type variable.
Example:
Copy Code code as follows:

int i=123;
Object box=i;
int j= (int) box;

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.