Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does unpacking in java mean?

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report