Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What does unpacking in java mean?

Shulou Source: shulou.com Published: 2022-06-02 05:43:11 10月29日 Update

This article mainly introduces "java boxing and unpacking is what means", in daily operation, I believe many people in java boxing and unpacking is what means there are doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, I hope to answer "java boxing and unpacking is what means" doubts help! Next, please follow the small series to learn together!

What can I learn from this article?

Why is it possible to report a null pointer exception when unpacking basic data types?

How is boxing and unpacking of primitive data types implemented?

automatic unpacking

First we have the following code:

public class Test{

static {

Integer integer = new Integer(1234);

int i = integer;

}

}

Decompile its corresponding class file to get the following bytecode:

public class Test {

public Test();

Code:

0: aload_0

1: invokespecial #1 // Method java/lang/Object. "":()V

4: return

LineNumberTable:

line 1: 0

static {};

Code:

0: new #2 // class java/lang/Integer

3: dup

4: sipush 1234

7: invokespecial #3 // Method java/lang/Integer. "":(I)V

10: astore_0

11: aload_0

12: invokevirtual #4 // Method java/lang/Integer.intValue:()I

15: istore_1

16: return

LineNumberTable:

line 3: 0

line 4: 11

line 5: 16

}

where int i = integer corresponds to the following bytecode:

11: aload_0

12: invokevirtual #4 // Method java/lang/Integer.intValue:()I

15: istore_1

As you can see, for int i = integer, this is actually equivalent to the following statement:

int i = integer.intValue();

Then when integer equals null, it becomes ((Integer)null).intValue(), which naturally throws a null pointer exception; the same principle applies to other primitive data types.

autoboxing

First you need the following code:

public class Test{

static {

int i = 1234;

Integer integer = i;

}

}

Decompile its corresponding class file to get the following bytecode:

public class Test {

public Test();

Code:

0: aload_0

1: invokespecial #1 // Method java/lang/Object. "":()V

4: return

LineNumberTable:

line 1: 0

static {};

Code:

0: sipush 1234

3: istore_0

4: iload_0

5: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;

8: astore_1

9: return

LineNumberTable:

line 3: 0

line 4: 4

line 5: 9

}

where Integer integer = i corresponds to the bytecode as follows:

4: iload_0

5: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;

8: astore_1

That is, for the Integer integer = i statement, it is actually equivalent to:

Integer integer = Integer.valueOf(i);

Since the primitive data type does not have null values in java, the corresponding autoboxing operation naturally has no risk of null pointer exceptions for autoboxing operations.

conclusion

Finally, we come to the following conclusion: for the auto-boxing operation of the basic data type, the Integer.valueOf(int) method is actually used, while for the auto-unpacking operation of the packaging type corresponding to the basic data type, the Integer#integer Value () method is used (note that this method is an ordinary method rather than a static method), so when the corresponding Integer object is null, there is a null pointer risk for the auto-unpacking operation;

At this point, the study of "what is the meaning of boxing and unpacking in java" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Tags: Boxing type data method meaning byte pointer learning practice actually code file more knowledge equivalence conclusion nature statement risk help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Microsoft macOS Redmi Shulou Tech Info