当前位置:网站首页>自动装箱与拆箱了解吗?原理是什么?
自动装箱与拆箱了解吗?原理是什么?
2022-07-03 09:15:00 【look-word】
自动装箱与拆箱了解吗?原理是什么?
什么是自动拆装箱?
- 装箱:将基本类型用它们对应的引用类型包装起来;
- 拆箱:将包装类型转换为基本数据类型;
举例:
Integer i = 10; //装箱
int n = i; //拆箱
字节码文件
L1
LINENUMBER 8 L1
ALOAD 0
BIPUSH 10
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
PUTFIELD AutoBoxTest.i : Ljava/lang/Integer;
L2
LINENUMBER 9 L2
ALOAD 0
ALOAD 0
GETFIELD AutoBoxTest.i : Ljava/lang/Integer;
INVOKEVIRTUAL java/lang/Integer.intValue ()I
PUTFIELD AutoBoxTest.n : I
RETURN
从字节码中,我们发现装箱其实就是调用了 包装类的valueOf()方法,拆箱其实就是调用了 xxxValue()方法。
因此,
Integer i = 10等价于Integer i = Integer.valueOf(10)int n = i等价于int n = i.intValue();
注意:如果频繁拆装箱的话,也会严重影响系统的性能。我们应该尽量避免不必要的拆装箱操作
private static long sum() {
// 应该使用 long 而不是 Long
Long sum = 0L;
for (long i = 0; i <= Integer.MAX_VALUE; i++)
sum += i;
return sum;
}
边栏推荐
- Idea remote breakpoint debugging jar package project
- CEF download, compile project
- GPIO port details, Hal library operation keys
- Nr--- Pusch I: sorting out the agreement process
- Jetson nano custom boot icon kernel logo CBOOT logo
- Leetcode daily question (1362. closest divisors)
- C language enumeration type
- 要选择那种语言为单片机编写程序呢
- 【男保姆式】教你打开第一个微信小程序
- 【力扣刷题笔记(二)】特别技巧,模块突破,45道经典题目分类总结,在不断巩固中精进
猜你喜欢

Nr-prach: access scenario and access process

Getting started with JMX, MBean, mxbean, mbeanserver

单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了

Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 5 --blinker_ MIOT_ MULTI_ Outside (lighting technology app + Xiaoai classmate control socket multiple jacks)
![[CSDN]C1訓練題解析_第三部分_JS基礎](/img/b2/68d53ad09688f7fc922ac65e104f15.png)
[CSDN]C1訓練題解析_第三部分_JS基礎
![[CSDN] C1 training problem analysis_ Part IV_ Advanced web](/img/ee/2e9756cc0e2e6eda83e1b2304c0bd6.png)
[CSDN] C1 training problem analysis_ Part IV_ Advanced web
![[CSDN]C1训练题解析_第四部分_Web进阶](/img/ee/2e9756cc0e2e6eda83e1b2304c0bd6.png)
[CSDN]C1训练题解析_第四部分_Web进阶
![[CSDN] C1 training problem analysis_ Part III_ JS Foundation](/img/b2/68d53ad09688f7fc922ac65e104f15.png)
[CSDN] C1 training problem analysis_ Part III_ JS Foundation
![[successful graduation] [1] - visit [student management information system]](/img/91/72cdea3eb3f61315595330d2c9016d.png)
[successful graduation] [1] - visit [student management information system]
![Successful graduation [2] - student health management system function development...](/img/91/72cdea3eb3f61315595330d2c9016d.png)
Successful graduation [2] - student health management system function development...
随机推荐
Project scope management__ Scope management plan and scope specification
What do software test engineers do? Pass the technology to test whether there are loopholes in the software program
Getting started with JMX, MBean, mxbean, mbeanserver
Directory and switching operation in file system
应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
UCI and data multiplexing are transmitted on Pusch - Part I
GPIO port details, Hal library operation keys
要选择那种语言为单片机编写程序呢
Solve editor MD uploads pictures and cannot get the picture address
【顺利毕业】[1]-游览 [学生管理信息系统]
端午节快乐!—— canvas写的粽子~~~~~
uniapp 实现微信小程序全局分享及自定义分享按钮样式
Code word in NR
Development of electrical fire system
The third paper of information system project manager in soft examination
[graduation successful] [1] - tour [Student Management Information System]
Runtime.getRuntime().gc() 和 Runtime.getRuntime().runFinalization() 的区别
Implementing distributed lock with redis
Introduction to chromium embedded framework (CEF)
UCI and data multiplexing are transmitted on Pusch (Part VI) -- LDPC coding