当前位置:网站首页>自动装箱与拆箱了解吗?原理是什么?
自动装箱与拆箱了解吗?原理是什么?
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;
}
边栏推荐
- Flink learning notes (IX) status programming
- numpy. Reshape() and resize() functions
- Leetcode daily question (1362. closest divisors)
- How MySQL modifies null to not null
- Project cost management__ Cost management technology__ Article 8 performance review
- Education is a pass and ticket. With it, you can step into a higher-level environment
- When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
- 对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
- Characteristics of PUCCH formats
- Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
猜你喜欢
Development of electrical fire system
Fundamentals of Electronic Technology (III)_ Chapter 2 principle of amplification circuit__ Crystal triode and field effect triode
单片机学到什么程度能找到工作,这个标准不好量化
Successful graduation [2] - student health management system function development...
Characteristics of PUCCH formats
[CSDN] C1 training problem analysis_ Part IV_ Advanced web
[CSDN]C1训练题解析_第四部分_Web进阶
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
STM32 serial port usart1 routine
Leetcode daily question (931. minimum falling path sum)
随机推荐
Uncle Wang's blog directory [constantly updating]
UCI and data multiplexing are transmitted on Pusch - Part I
Code word in NR
Leetcode daily question (745. prefix and suffix search)
STM32 port multiplexing and remapping
NR PUCCH format0 sequence generation and detection mechanism
Leetcode daily question (968. binary tree cameras)
All processes of top ten management in project management
307. Range Sum Query - Mutable
Flink learning notes (VIII) multi stream conversion
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
Leetcode daily question (2212. maximum points in an archery competition)
Successful graduation [3]- blog system update...
Characteristics of PUCCH formats
You need to use MySQL in the opening experiment. How can you forget the basic select statement? Remedy is coming~
Leetcode daily question (931. minimum falling path sum)
Jestson nano downloads updated kernel and DTB from TFTP server
Nr--- Pusch I: sorting out the agreement process
UCI and data multiplexing are transmitted on Pusch (Part VI) -- LDPC coding
嵌入式系统没有特别明确的定义