当前位置: 首页 > article >正文

关于Integer和int的拆箱和装箱

什么是拆箱和装箱

Integer称为包装类,所以自动装箱自然是相对于int来说,转化为Integer。相反,拆箱就是Integer转int。

拆箱和装箱的分类

隐式/自动
装箱

定义一个Integer对象,赋一个int值,会自动转化为Integer

Integer boxedInt = 10;
拆箱

把刚刚定义的Integer直接赋值给int,会自动转化为int

int autoUnboxedInt = boxedInt;
显式
装箱

先定义一个int变量,再调用integer中的静态方法转为Integer

int primitiveInt = 5;  
Integer boxedInt = Integer.valueOf(primitiveInt); // 显式装箱 
拆箱

先定义一个Integer变量,再调用内置方法转为int

Integer boxedInt = 10; // 由于存在自动装箱,这里实际上创建了一个Integer对象  
int primitiveInt = boxedInt.intValue(); // 显式拆箱  


http://www.kler.cn/news/343159.html

相关文章:

  • HiRT | 异步控制策略,告别VLA时延问题
  • 【RPC】—Thrift协议 VS Protobuf
  • StringEntity 用于将字符串内容作为 HTTP 请求实体(请求体)
  • docker+mysql创建用户名密码_docker里面的mysql 更换密码
  • Linux与RTOS的区别
  • SQL第13课——创建高级联结
  • Spring Security之RememberMe
  • 美发店管理革新:SpringBoot系统的应用
  • 【低代码】前端低代码开发日记2_遇到的问题(2)变量绑定
  • python 实现BFS判断是否是二分图Bipartite算法
  • 机器学习和深度学习的差别
  • Elasticsearch 入门
  • 数字马力ai面试题
  • 推荐一个边缘物联网平台
  • Streamlit:用Python快速构建交互式Web应用
  • 宝塔 进程守护管理器 神坑,再次跌入。thinkphp-queue队列 勤勤学长
  • 跨集群复制:在Amazon OpenSearch服务中实现数据同步
  • 牛上脑和各类牛排的叫法,不要土老帽了~
  • NRF24L01无线通信模块学习 来自正点原子标准库
  • Unity3D 动画回调函数详解