当前位置:网站首页>自動裝箱與拆箱了解嗎?原理是什麼?
自動裝箱與拆箱了解嗎?原理是什麼?
2022-07-03 09:53: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;
}
边栏推荐
- Vector processor 9_ Basic multilevel interconnection network
- The third paper of information system project manager in soft examination
- Fundamentals of Electronic Technology (III)__ Chapter 6 combinational logic circuit
- Nr--- Pusch I: sorting out the agreement process
- 单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了
- [CSDN]C1训练题解析_第二部分_Web基础
- 学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
- Process communication - semaphore
- IDEA远程断点调试jar包项目
- Leetcode daily question (931. minimum falling path sum)
猜你喜欢

There is no specific definition of embedded system

Shell logic case

Fundamentals of Electronic Technology (III)_ Chapter 2 principle of amplification circuit__ Crystal triode and field effect triode

MySQL Data Definition Language DDL common commands

Project cost management__ Topic of comprehensive calculation

【力扣刷题笔记(二)】特别技巧,模块突破,45道经典题目分类总结,在不断巩固中精进

Definition and use of enum in C language

CEF download, compile project

Exception handling of arm

Design and development of biological instruments
随机推荐
MySQL 数据库基础知识(系统化一篇入门)
要選擇那種語言為單片機編寫程序呢
SSB Introduction (PbCH and DMRs need to be supplemented)
Leetcode daily question (1856. maximum subarray min product)
Flink learning notes (XI) table API and SQL
顺利毕业[3]-博客系统 更新中。。。
The third paper of information system project manager in soft examination
Epollet lessons
QT qcombobox QSS style settings
一个可执行的二进制文件包含的不仅仅是机器指令
Qt QComboBox QSS样式设置
Leetcode daily question (2212. maximum points in an archery competition)
Education is a pass and ticket. With it, you can step into a higher-level environment
Successful graduation [3]- blog system update...
Getting started with JMX, MBean, mxbean, mbeanserver
MySQL environment variable configuration
I didn't think so much when I was in the field of single chip microcomputer. I just wanted to earn money to support myself first
Exception handling of arm
Project cost management__ Cost management technology__ Article 6 prediction
【顺利毕业】[1]-游览 [学生管理信息系统]