当前位置:网站首页>自动装箱与拆箱了解吗?原理是什么?
自动装箱与拆箱了解吗?原理是什么?
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;
}
边栏推荐
- Fundamentals of Electronic Technology (III)__ Chapter 6 combinational logic circuit
- Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
- 内存数据库究竟是如何发挥内存优势的?
- UCI and data multiplexing are transmitted on Pusch - Part I
- Leetcode daily question (2090. K radius subarray averages)
- SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers
- Project cost management__ Cost management technology__ Article 8 performance review
- Flink learning notes (XI) table API and SQL
- Runtime.getRuntime().gc() 和 Runtime.getRuntime().runFinalization() 的区别
- [successful graduation] [1] - visit [student management information system]
猜你喜欢
Schematic diagram and connection method of six pin self-locking switch
Electronic product design, MCU development, circuit cloning
Convert IP address to int
Flink CDC practice (including practical steps and screenshots)
Design and development of biological instruments
Difference of EOF
CEF下载,编译工程
学习开发没有捷径,也几乎不存在带路会学的快一些的情况
numpy. Reshape() and resize() functions
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 4 --blinker_ DHT_ WiFi (lighting technology app control + temperature and humidity data app display)
随机推荐
在三线城市、在县城,很难毕业就拿到10K
Development of electrical fire system
Leetcode daily question (2090. K radius subarray averages)
万字手撕七大排序(代码+动图演示)
Oracle数据库 SQL语句执行计划、语句跟踪与优化实例
In third tier cities and counties, it is difficult to get 10K after graduation
內存數據庫究竟是如何發揮內存優勢的?
Leetcode daily question (2305. fair distribution of cookies)
MySQL 数据库基础知识(系统化一篇入门)
[csdn] C1 analyse des questions de formation Partie III Bar _ JS Foundation
Idea remote breakpoint debugging jar package project
Eight working modes of stm32gpio and chip naming rules
单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了
Electronic product design
Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()
Desktop icon recognition based on OpenCV
Fundamentals of Electronic Technology (III)_ Chapter 2 principle of amplification circuit__ Crystal triode and field effect triode
UCI and data multiplexing are transmitted on Pusch - Part I
MySQL data manipulation language DML common commands
顺利毕业[3]-博客系统 更新中。。。