Unpacking and boxing in Java

Source: Internet
Author: User

First of all, in Java, the wrapper class, the Java language is an object-oriented language, but the basic data type in Java is not object-oriented, which in the actual use of a lot of inconvenience (for example, we can not directly think of the collection collections into the original type value, Because the collection only receives objects). To address this deficiency, the design class is represented by a corresponding class for each of the basic data types, so that the classes corresponding to the eight and basic data types are collectively referred to as wrapper classes (Wrapper Class).

Basic data types

Packing class

Byte

Byte

Boolean

Boolean

Short

Short

Char

Character

Int

Integer

Long

Long

Float

Float

Double

Double

Automatic boxing is that Java automatically converts the original type to the corresponding object, such as converting an int variable into an integer object, which is called boxing, and the inverse of converting an integer object to an int object, the process is called unpacking. And because this process occurs automatically, it is also called automatic Boxing, automatic unpacking.

New Arraylist<integer>(); Arraylist.add (1);  // Auto-Boxing int ---Integer int number = arraylist.get (0);     // Automatic Unpacking   Integer   -to-int

Simply put, boxing is the automatic conversion of the basic type to the package type, unpacking is the automatic conversion of the packaging type into the basic type.

In the process of automatic boxing, the system performs a

Integer total = integer.valueof (1);

During the automatic unpacking process, the system performs a

Number = Total.intvalue ();

Disadvantages:

In some cases, automatic boxing creates extra objects if you are not aware of them, affecting the performance of the program.

Integer sum = 0for (int i=1000; i<5000; i++) {   sum+ =i;}

In the code above, Sum+=i can be regarded as sum = Sum +i, but this + operator is not applicable to the integer object, first sum for the automatic unpacking operation, the addition of the number of operations, the final occurrence of automatic boxing into an integer object.

The internal changes are as follows:

sum = Sum.intvalue () +new Integer (result);

Since our declared sum is of type integer, nearly 4,000 useless objects are created in the loop above, which in such a large loop degrades the performance of the program and increases the amount of garbage collected. So when we're programming, we need to be aware of this and correctly declare variable types to avoid performance problems caused by automatic boxing.

Inside the integer class are some methods related to int operations, and some of the more common methods are described below:

A, parseint method

public static int parseint (String s)

The function of this method is to convert a numeric string to an int value. Converting a string to the corresponding int number is a common operation in future interface programming. Examples of use are:

String s = "123";

int n = integer.parseint (s);

The value of the INT variable n is 123, which actually implements the conversion between the string and int, and if the string contains not all numeric characters, the execution of the program will be an exception.

Another parseint method:

public static int parseint (String s, int radix)

The implementation converts the string to an int as specified by the parameter radix, using the following example:

Converts the string "120" to int by decimal, the result is 120

int n = integer.parseint ("120", 10);

Converts the string "12" to int by 16, the result is 18

int n = integer.parseint ("12", 16);

Converts the string "FF" to int by 16, the result is 255

int n = integer.parseint ("FF", 16);

This allows for a more flexible conversion.

B, ToString Method

public static String toString (int i)

The function of this method is to convert the int type to the corresponding string type.

Use the sample code as follows:

int m = 1000;

String s = integer.tostring (M);

The value of the string s is "1000".

Another ToString rule implements converting an int value to a specific binary string:

public static int parseint (String s, int radix)

Use the sample code as follows:

int m = 20;

String s = integer.tostring (M);

The value of the string s is "14".

In addition, one of the problems encountered in the first two days

  int i = integer.valueof ("222");        System.out.println (i);

The problem was found with findbugs, with redundant unboxing and boxing operations, because Interger.valuesof ("") returned an integer object and then unpacking to the int type.

See a few examples

        inti = 128; Integer I2= 128; Integer i3=NewInteger (128); //integer is automatically unboxing to int, so trueSystem.out.println (i = = i2);//trueSystem.out.println (i = = i3);//trueSystem.out.println ("**************"); System.out.println (I2= = i3);//falseInteger i5 = 127;//when Java is compiled, it is translated into Integer i5 = integer.valueof (127);Integer I6 = 127; SYSTEM.OUT.PRINTLN (i5= = I6);//true        /*Integer i5 = 128;        Integer I6 = 128; SYSTEM.OUT.PRINTLN (i5 = = I6);//false*/Integer ii5 =NewInteger (127); SYSTEM.OUT.PRINTLN (i5= = Ii5);//falseInteger i7 =NewInteger (127); Integer i8=NewInteger (123); SYSTEM.OUT.PRINTLN (i7= = i8);//false

To sum it up, if the integer and int types are compared, then the integer type is disassembled to the int type and then numerically compared, if the integer i = new Integer (127), this creates a new integer object, So the actual comparison is two objects, basically will return false (of course, if compared with the int type is also the result of the return of a numerical comparison). If it is an Integer I1 = 127,integer I2 = 127, this comparison, if less than 128 will return true, greater than or equal to 128 will return false.

Why does this happen, when it comes to integer i = 127, which executes the boxing operation, actually executes the integer.valueof (127), let's take a look at the ValueOf function

 Public Static Integer valueOf (int  i) {    return  new Integer (i): Small_values[i + +) ;}

It will first determine the size of I: If I is less than-128 or greater than or equal to 128, create an integer object, otherwise execute Small_values[i + 128].

Private Static Final New INTEGER[256];

Their values are within the range of (-128,128], they get the same object in the Small_values array small_values[228], and they refer to the same integer object.

Unpacking and boxing 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: 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.