当前位置:网站首页>自動裝箱與拆箱了解嗎?原理是什麼?
自動裝箱與拆箱了解嗎?原理是什麼?
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;
}
边栏推荐
- Flink CDC practice (including practical steps and screenshots)
- [successful graduation] [1] - visit [student management information system]
- Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
- Raspberry pie installation SciPy
- 307. Range Sum Query - Mutable
- MySQL 数据库基础知识(系统化一篇入门)
- Leetcode daily question (2109. adding spaces to a string)
- Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
- 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
- 嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
猜你喜欢

UCI and data multiplexing are transmitted on Pusch - placement of data and UCI positions (Part III)

对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门

Nr--- Pusch I: sorting out the agreement process

Education is a pass and ticket. With it, you can step into a higher-level environment

Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 4 --blinker_ DHT_ WiFi (lighting technology app control + temperature and humidity data app display)

新系列单片机还延续了STM32产品家族的低电压和节能两大优势

Stm32-hal library learning, using cubemx to generate program framework

Error output redirection

NR technology -- MIMO
![【順利畢業】[1]-遊覽 [學生管理信息系統]](/img/91/72cdea3eb3f61315595330d2c9016d.png)
【順利畢業】[1]-遊覽 [學生管理信息系統]
随机推荐
The third paper of information system project manager in soft examination
端午节快乐!—— canvas写的粽子~~~~~
Leetcode daily question (2212. maximum points in an archery competition)
uniapp 实现微信小程序全局分享及自定义分享按钮样式
单片机学到什么程度能找到工作,这个标准不好量化
[combinatorics] Introduction to Combinatorics (combinatorial thought 2: mathematical induction | mathematical induction promotion | multiple induction thought)
Convert IP address to int
Code word in NR
Introduction to chromium embedded framework (CEF)
万字手撕七大排序(代码+动图演示)
MySQL data manipulation language DML common commands
PIP references domestic sources
顺利毕业[3]-博客系统 更新中。。。
手机都算是单片机的一种,只不过它用的硬件不是51的芯片
Project cost management__ Plan value_ Earned value_ Relationship among actual cost and Countermeasures
Flink learning notes (IX) status programming
编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解
Characteristics of PUCCH formats
Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving
开学实验里要用到mysql,忘记基本的select语句怎么玩啦?补救来啦~