java 基本数据类型的自动拆箱与装箱

 2023-09-08 阅读 18 评论 0

摘要:——> -128~127之间的特殊性。为什么要这样设计,好处? ——> 享元模式(Flyweight Pattern):享元模式的特点是,复用我们内存中已存在的对象,降低系统创建对象实例。 自动装箱: Integer num1 = 12; 自动拆箱

——>  -128~127之间的特殊性。为什么要这样设计,好处?
——>  享元模式(Flyweight Pattern):享元模式的特点是,复用我们内存中已存在的对象,降低系统创建对象实例。

自动装箱:
Integer num1 = 12;

自动拆箱:
System.out.println(num1 + 12);
基本数据类型的对象缓存:
Integer num1 = 12;
Integer num2 = 12;
System.out.println(num1 == num2);
 
Integer num3 = 129;
Integer num4 = 129;
System.out.println(num3 == num4);//false
 
Integer num5 = Integer.valueOf(12);
Integer num6 = Integer.valueOf(12);
System.out.println(num5 == num6);//false


我的总结:对于享元模式,将最常用的封装以便于我们可以复用!

integer装箱拆箱、转载于:https://www.cnblogs.com/fanweisheng/p/11137678.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/20286.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息