当前位置:网站首页>自動裝箱與拆箱了解嗎?原理是什麼?
自動裝箱與拆箱了解嗎?原理是什麼?
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;
}
边栏推荐
- MySQL data manipulation language DML common commands
- STM32 serial communication principle
- [CSDN] C1 training problem analysis_ Part II_ Web Foundation
- 内存数据库究竟是如何发挥内存优势的?
- Fundamentals of Electronic Technology (III)__ Chapter 6 combinational logic circuit
- Leetcode daily question (2232. minimize result by addressing parents to expression)
- 顺利毕业[2]-学生健康管理系统 功能开发中。。。
- Leetcode daily question (931. minimum falling path sum)
- Code word in NR
- Project cost management__ Cost management technology__ Article 7 completion performance index (tcpi)
猜你喜欢
SSB Introduction (PbCH and DMRs need to be supplemented)
当你需要使用STM32某些功能,而51实现不了时, 那32自然不需要学
Intelligent home design and development
【順利畢業】[1]-遊覽 [學生管理信息系統]
Mysql database underlying foundation column
Design and development of biological instruments
Project cost management__ Cost management technology__ Article 6 prediction
Nodemcu-esp8266 development board to build Arduino ide development environment
numpy. Reshape() and resize() functions
Flink learning notes (XI) table API and SQL
随机推荐
Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
Design and development of biological instruments
When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
Nr-prach: access scenario and access process
MySQL Data Definition Language DDL common commands
Project cost management__ Cost management technology__ Article 8 performance review
GPIO port details, Hal library operation keys
嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
【22毕业季】我是毕业生yo~
应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
没有多少人能够最终把自己的兴趣带到大学毕业上
Leetcode daily question (2212. maximum points in an archery competition)
2020-08-23
Fundamentals of Electronic Technology (III)__ Logic gate symbols in Chapter 5
Leetcode daily question (985. sum of even numbers after queries)
STM32 interrupt switch
Fundamentals of Electronic Technology (III)_ Chapter 2 principle of amplification circuit__ Crystal triode and field effect triode
How does the nr-prach receiver detect the relationship between prembleid and Ta
Leetcode daily question (2305. fair distribution of cookies)
MYSQL数据库底层基础专栏