[Java development path] (23) packing and unpacking

Source: Internet
Author: User

[Java development path] (23) packing and unpacking

1. Introduction

You are familiar with basic data types, such as int, float, double, boolean, and char. The basic data type does not have the characteristics of objects. For example, the basic data type cannot call methods and has simple functions ..., java provides a packaging class for each basic data type so that we can operate on the basic data type just like an object.

The object packaging class remains unchanged. That is, once a packaging class is constructed, the value in the packaging class cannot be changed. At the same time, the object packaging class is still final, so their subclasses cannot be defined.

Suppose we want to define an integer array list, and the type parameter in angle brackets cannot be of the basic type, that is, it cannot be written as an ArrayList. . The object packaging class is used here. We can declare an array list of Integer objects.


  1. ArrayList list = new ArrayList ();

Note:

ArrayList The efficiency is much lower than the int [] array, but the ArrayList is used because the convenience of operations by programmers is more important than the execution efficiency.

Perform the following operations:


  1. list.add(3);

Will be automatically converted:


  1. list.add(Integer.valueOf(3));

This transformation is automatically packed.

Conversely, when an Integer object is assigned an int value, it is automatically split:


  1. int n = list.get(i)

Will be automatically converted:


  1. int n = list.get(i).intValue();

Note:

Packing and unpacking are approved by the compiler, rather than virtual machines. The compiler inserts necessary method calls when generating the class bytecode. The VM only executes these bytecode.

Using the value object packaging class has one benefit:

Some basic methods can be placed in the packaging class.


  1. int x = Integer.parseInt(s);
There is no relation to the Integer object. parseInt is only a static method, but the Integer class is a good place to place this method.

2. Category

Packing: converts the basic type into a packaging class so that it has the object nature. It can also be divided into manual packing and automatic packing.


  1. package com.qunar.test;
  2.  
  3. public class HelloWorld {
  4. public static void main(String[] args) {
  5. // Define an int basic type value
  6. int num = 14;
  7. // Manual packing
  8. Integer x = new Integer(num);
  9. // Automatic Packing
  10. Integer y = num;
  11. System.out.println("x->"+x+" y->"+y);
  12. }
  13. }

Binning: In contrast to packing, you can convert a package object to a value of the basic type, which can be divided into manual unpacking and automatic unpacking.


  1. package com.qunar.test;
  2.  
  3. public class HelloWorld {
  4. public static void main(String[] args) {
  5. // Define an Integer packaging class
  6. Integer integer = new Integer(8);
  7. // Manually unpack
  8. int x = integer.intValue();
  9. // Automatically unpack
  10. int y = integer;
  11. System.out.println("x->"+x+" y->"+y);
  12. }
  13. }

3. Analysis

Disadvantages of automatic packing:

There is a problem with automatic packing, that is, the automatic packing operation in a loop. The following example will create redundant objects, affecting the program performance:


  1. Integer sum = 0;
  2. for(int i=1000; i<5000; i++){
  3. sum+=i;
  4. }

The above code sum + = I can be regarded as sum = sum + I, but the + operator is not applicable to Integer objects. First, sum performs the automatic unpacking operation to add values, finally, the automatic packing operation is converted into an Integer object. The internal changes are as follows:


  1. sum = sum.intValue() + i;
  2. Integer sum = new Integer(result);

Therefore, when programming, we need to pay attention to this point and correctly declare the variable type to avoid performance problems caused by automatic packing.

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.