当前位置:网站首页>自动装箱与拆箱了解吗?原理是什么?
自动装箱与拆箱了解吗?原理是什么?
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;
}
边栏推荐
- 编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解
- Chromium Embedded Framework (CEF) 介绍
- 一个可执行的二进制文件包含的不仅仅是机器指令
- Matlab reads hexadecimal numbers and converts them into signed short
- [CSDN] C1 training problem analysis_ Part III_ JS Foundation
- [CSDN] C1 training problem analysis_ Part IV_ Advanced web
- Flink learning notes (XI) table API and SQL
- PIP references domestic sources
- Project cost management__ Topic of comprehensive calculation
- MySQL environment variable configuration
猜你喜欢

How MySQL modifies null to not null

MySQL data manipulation language DML common commands

You need to use MySQL in the opening experiment. How can you forget the basic select statement? Remedy is coming~
![顺利毕业[2]-学生健康管理系统 功能开发中。。。](/img/91/72cdea3eb3f61315595330d2c9016d.png)
顺利毕业[2]-学生健康管理系统 功能开发中。。。

Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()

嵌入式系统没有特别明确的定义

Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
![Successful graduation [2] - student health management system function development...](/img/91/72cdea3eb3f61315595330d2c9016d.png)
Successful graduation [2] - student health management system function development...

Flink learning notes (IX) status programming

一个可执行的二进制文件包含的不仅仅是机器指令
随机推荐
Process communication - semaphore
Code word in NR
Epollet lessons
当你需要使用STM32某些功能,而51实现不了时, 那32自然不需要学
Development of electrical fire system
Project cost management__ Plan value_ Earned value_ Relationship among actual cost and Countermeasures
Matlab reads hexadecimal numbers and converts them into signed short
Implementing distributed lock with redis
MySQL 数据库基础知识(系统化一篇入门)
我想各位朋友都应该知道学习的基本规律就是:从易到难
STM32 interrupt priority management
Getting started with shell programming
Fundamentals of Electronic Technology (III)__ Chapter 1 resistance of parallel circuit
Shell logic case
[22 graduation season] I'm a graduate yo~
Project cost management__ Cost management technology__ Article 7 completion performance index (tcpi)
Raspberry pie installation SciPy
內存數據庫究竟是如何發揮內存優勢的?
How does the nr-prach receiver detect the relationship between prembleid and Ta
[CSDN] C1 training problem analysis_ Part IV_ Advanced web