当前位置:网站首页>自動裝箱與拆箱了解嗎?原理是什麼?
自動裝箱與拆箱了解嗎?原理是什麼?
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;
}
边栏推荐
- Electronic product design
- An executable binary file contains more than machine instructions
- Leetcode daily question (2109. adding spaces to a string)
- Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()
- Idea remote breakpoint debugging jar package project
- Code word in NR
- Getting started with shell programming
- Exception handling of arm
- Leetcode daily question (2232. minimize result by addressing parents to expression)
- [CSDN]C1训练题解析_第二部分_Web基础
猜你喜欢

Leetcode daily question (931. minimum falling path sum)

Characteristics of PUCCH formats

Intelligent home design and development
![顺利毕业[3]-博客系统 更新中。。。](/img/91/72cdea3eb3f61315595330d2c9016d.png)
顺利毕业[3]-博客系统 更新中。。。
![Successful graduation [2] - student health management system function development...](/img/91/72cdea3eb3f61315595330d2c9016d.png)
Successful graduation [2] - student health management system function development...

Schematic diagram and connection method of six pin self-locking switch

Comment la base de données mémoire joue - t - elle l'avantage de la mémoire?

我想各位朋友都应该知道学习的基本规律就是:从易到难

Exception handling of arm

Oracle database SQL statement execution plan, statement tracking and optimization instance
随机推荐
Nr-prach:prach format and time-frequency domain
Runtime.getRuntime().gc() 和 Runtime.getRuntime().runFinalization() 的区别
Leetcode daily question (931. minimum falling path sum)
STM32 interrupt priority management
A lottery like scissors, stone and cloth (C language)
Introduction to chromium embedded framework (CEF)
Nr--- Pusch I: sorting out the agreement process
Project cost management__ Topic of comprehensive calculation
Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()
How does the nr-prach receiver detect the relationship between prembleid and Ta
The number of weak characters in the game (1996)
【22毕业季】我是毕业生yo~
PRACH --- originator
Hal library sets STM32 clock
Leetcode daily question (1856. maximum subarray min product)
MySQL的简单使用(增删改查)
Leetcode daily question (2109. adding spaces to a string)
Learn the contents of 5g toolbox supporting NR through the NR resources provided by MATLAB
An executable binary file contains more than machine instructions
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation