java automatically with the goods


title: "java automatically with the goods"
Tags:

  • Java

The basic data types into objects encapsulation process is called packing (boxing), and vice versa corresponding to the basic data types, called packaging conversion unboxing (Unboxing) of the basic data types of the process;

The basic difference between the types of data to other objects

Basic data types

Java is a strongly typed object-oriented language, but that everything is an object, a part of Java in the most frequently used data structures it like python is not object-oriented, they are the basic data types, also called built-in type, they in the stack storage, compared with other newobjects created, more efficient, Java has nine basic data types, divided into five categories

Types of Identifier Remark
Integer byte, short, int, long
floating point float, dauble
character char
Boolean boolean
air void Inoperable

Range of basic data types

Integer range

  • byte: one byte, i.e. 8 bits, the maximum as a sign bit, only the valid bit 7 (using the complement code storage).
最大值:0,111 1111(127)
最小值:1,000 0000(-128)

How worthwhile?
The highest one is the sign bit, which is a fixed positive number represented by 0, represented by a negative number, then the latter 7 is a seven maximum, the minimum is 0 7, which is represented by a byte can be represented complement the maximum and minimum number, the complement conversion to the original code (n is the number of the source of complement, the complement becomes negative source negated plus one) and then converted to decimal.

  • short: two bytes, 16-bit, the effective 15
最大值:2^15 -1: 32,767
最小值:-(2^15): -32,768
  • int: 4 bytes, the maximum value of {2 ^ 31 - 1] (2,147,483,647), the minimum value of [-2] 31 ^ (- 2,147,483,648)
  • long: 8 bytes, the maximum value of {2 ^ 63 - 1] (9,223,372,036,854,775,807), the minimum value of [-2] 63 ^ (- 9,223,372,036,854,775,808)

type of packaging

Other Java objects are inherited from the object, with its own properties and methods, in order to facilitate substantially basic data types, and other objects of the operation, provides a Java package type corresponding to each elementary data type,

Basic data types Wrapper class
byte Byte
boolean Boolean
short Short
char Character
int Integer
long Long
float Float
double Double
  • Why use Package Type

Java is an object-oriented language, most operations are directed at an object, such as a container, the largest container can be stored in the range is the object, and basic data types are not covered, then he can not be stored in the container, in order to to solve this problem, the basic data types must be "wrapped" to keep him as an object to participate in programming.

Boxing and unboxing

The basic data types into a packaging process is called packing objects, whereas the basic data type conversion target process is called unpacking.
After Java SE5, in order to simplify development, an automatic mechanism unboxing, automatically converts Java basic data types at the appropriate time and the type of packaging, such as:

public class Demo1 {
    public static void main(String[] args) {
        Integer integer = 3; // 自动装箱
        int i = integer;  // 自动拆箱
    }
}

Decompile obtained by Javap

  public static void main(java.lang.String[]);
    Code:
       0: iconst_3
       1: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       4: astore_1
       5: aload_1
       6: invokevirtual #3                  // Method java/lang/Integer.intValue:()I
       9: istore_2
      10: return

When auto-boxing, in fact, is to call a wrapper class valueOf()methods, and in the auto-unboxing is called a wrapper class intValue()methods, so if before JavaSE5, there is no automatic mechanism with the goods, the above code we need to write

public class Demo1 {
    public static void main(String[] args) {
        Integer integer = Integer.valueOf(3); // 装箱
        int i = Integer.intValue(integer);  // 拆箱
    }
}

In addition to int and Integer, other basic types of automatic conversion and packaging the same, when the call packing valueOf()method, called when unpacking xxxValue()method.

When autoboxing

1. initialization, assignment, the function returns

When the basic data types assigned to a packaging or substantially return value data type function declaration, but the type of packaging required to return automatically as a function of packing, such as the above example

2. The basic data types into a container

public void func2(){
        List<Integer> list = new ArrayList<>();
        list.add(1);
    }

After disassembly

  public void func2();
    Code:
       0: new           #4                  // class java/util/ArrayList
       3: dup
       4: invokespecial #5                  // Method java/util/ArrayList."<init>":()V
       7: astore_1
       8: aload_1
       9: iconst_1
      10: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      13: invokeinterface #6,  2            // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      18: pop
      19: return

In step 10, using autoboxing

When auto-unboxing

1. initialization, assignment, the function returns

When the packaging object is assigned to the basic data types of variables that are automatically unpacking (function returns the same principle)

2. Packaging type do arithmetic

Arithmetic (including comparative size) is the basic data type, whether it is done so the operation will be between basic data types or types of packaging and packaging and packaging type is converted into two types of elementary data types

public void func3(){
        Integer integer = 3;
        int i = 1;
        Integer integer1 = 1;
        boolean b1 = integer > i; // 基本数据类型与包装类型比较大小
        boolean b2 = integer > integer1; // 两个包装类型比较大小
    }

After disassembly

public void func3();
    Code:
       0: iconst_3
       1: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       4: astore_1
       5: iconst_1
       6: istore_2
       7: iconst_1
       8: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      11: astore_3
      12: aload_1
      13: invokevirtual #3                  // Method java/lang/Integer.intValue:()I
      16: iload_2
      17: if_icmple     24
      20: iconst_1
      21: goto          25
      24: iconst_0
      25: istore        4
      27: aload_1
      28: invokevirtual #3                  // Method java/lang/Integer.intValue:()I
      31: aload_3
      32: invokevirtual #3                  // Method java/lang/Integer.intValue:()I
      35: if_icmple     42
      38: iconst_1
      39: goto          43
      42: iconst_0
      43: istore        5
      45: return

By step 13, the basic data types and compare the size of package types are converted into two basic types of data and then comparing
through step 28, 32, two type of packaging described will be converted to compare the size of basic data types

Ordinary arithmetic, too

public void func4() {
        Integer integer = 3;
        int i = 1;
        Integer integer1 = 1;
        int s = integer + integer1;
        int s1 = integer + i;
    }
public void func4();
    Code:
      //...
      13: invokevirtual #3                  // Method java/lang/Integer.intValue:()I
      16: aload_3
      17: invokevirtual #3                  // Method java/lang/Integer.intValue:()I
      20: iadd
      21: istore        4
      23: aload_1
      24: invokevirtual #3                  // Method java/lang/Integer.intValue:()I
      //...

3. The operation trinocular

If the second operation is a trinocular three basic data type is the other type of packaging, it will automatically unpacking data into two basic types

public void func5() {
    boolean flag = true;
    Integer i = 8;
    int j;
    j = 3;
    int k = flag ? i: j;
}
  public void func5();
    Code:
       0: iconst_1
       1: istore_1
       2: bipush        8
       4: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       7: astore_2
       8: iconst_3
       9: istore_3
      10: iload_1
      11: ifeq          21
      14: aload_2
      15: invokevirtual #3                  // Method java/lang/Integer.intValue:()I
      18: goto          22
      21: iload_3
      22: istore        4
      24: return

Since i is a type of packaging, j is the basic data type, line 14 in the i would be automatically unpacking basic data types (three head should not operation returns int), so do the operational three head should be noted that, in particular, and basic data types are mixed object, if the object has not been assigned, may lead to an NPL (null pointer exception)

Only one type of data is a basic, a package is to be automatically unpacking class object, the two objects are not removed.

Cache problem when boxing and unboxing

public class Demo2 {
    public static void main(String[] args) {
        Integer a1 = 1;
        Integer a2 = 1;
        int a3 = 1;
        System.out.println(a1 == a2);  // true
        System.out.println(a1.equals(a2));  // true
        System.out.println(a1 == a3);  // true
        System.out.println(a1.equals(a3));  // true
    }
}
public class Demo2 {
    public static void main(String[] args) {
        Integer a1 = 133;
        Integer a2 = 133;
        int a3 = 133;
        boolean b1 = a1 == a2;
        boolean b2 = a1.equals(a2);
        boolean b3 = a1 == a3;
        boolean b4 = a1.equals(a3);
        System.out.println(b1);  // false
        System.out.println(b2);  // true
        System.out.println(b3);  // true
        System.out.println(b4);  // true
    }
}

Two different results, because that is a caching issue with the automatic dismantling me, when we first used Integer, Java will initialize a Integer[] cachethen by the number of cycles between -128 to 127 is added to the cache, if the new new Integer value within this range, the direct return this good object is created, equals()the comparison value, ==the comparison is not the same object, so any case, equals the result is true, but ==between -128 to 127 is true, beyond this range is false.

This is similar to a small integer python in the pool, but the scope of the python is [-5, 256]

In addition to integer between [-128, 127], boolean values of the two, as well as \u0000to \u007fthe character also between the constant pool.

to sum up

  1. What is the wrapper class

In order to facilitate operation of the basic data types, a wrapper class for each basic type, they packaged into a target basic data types

  1. What is the packing, unpacking

The basic data types into a packaging process is called packaging containers (using a wrapper class valueOf()方法)
converting a basic packaging process is called unpacking data types (using a wrapper class xxxValue()方法)

  1. What is auto-boxing / unboxing

Java SE5 one kind of the basic data types introduced automatically converted to packaging in certain cases / packaging automatically converted to the basic data type of mechanism

  1. When autoboxing

  2. Integer a = 5

  3. Function return value

  4. When the basic data types added to the vessel

  5. When auto-unboxing

  6. Initialization, assignment, function return values

  7. When the three head operational parameters of both the second and third packaging, there are basic data types

  8. Arithmetic operations, magnitude comparison

  9. What is cached automatic entry box

The first time you use certain packaging type, Java will create a buffer pool, after packaging class object every time you need, you will find the first cache pool there, there are direct return, not only to create. Buffer pool range:

Integer: [--128, 127]
Boolean: [true, false]
characters: [ \u0000, \u007f]

reference

What is Java automatic entry boxes
in Java caching mechanism integer

Published 63 original articles · won praise 33 · views 10000 +

Guess you like

Origin blog.csdn.net/zjbyough/article/details/104033135