Java装箱与拆箱的简单理解

本文详细介绍了Java中基本数据类型与引用数据类型之间的转换,包括装箱与拆箱的过程及其实现方式。特别强调了Integer等包装类的valueOf方法在缓存方面的优化策略。

Integer --> int 拆箱

int --> Integer 装箱

java数据类型的引用数据类型转为基本数据类型,拆箱。基本数据类型转为引用数据类型,装箱。

引用类型有:Integer,Byte, Short, Character, Long, 还有Double, Float

对应的分别为:int, byte, short, char, long, 还有double, float

装箱时调用的函数为XX.valueOf(xx);

拆箱时调用的函数为xx.xxValue();


以下均为java SE8版本

Integer 的valueOf(int)官方文档介绍:

Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象。


Byte的valueOF(byte)官方文档介绍:

Returns a Byte instance representing the specified byte value. If a new Byte instance is not required, this method should generally be used in preference to the constructor Byte(byte), as this method is likely to yield significantly better space and time performance since all byte values are cached.


Short的valuOf(short)的官方文档介绍:

Returns a  Short instance representing the specified  short value. If a new  Short instance is not required, this method should generally be used in preference to the constructor  Short(short), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

Character的valueOf的官方文档介绍:

Returns a  Character instance representing the specified  char value. If a new  Character instance is not required, this method should generally be used in preference to the constructor  Character(char), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range  '\u0000' to  '\u007F', inclusive, and may cache other values outside of this range.


Long的官方文档介绍:

Returns a Long instance representing the specified long value. If a new Long instance is not required, this method should generally be used in preference to the constructor Long(long), as this method is likely to yield significantly better space and time performance by caching frequently requested values. Note that unlike the corresponding method in the Integer class, this method is not required to cache values within a particular range.

Double的valueOf(double)的官方文档介绍:

Returns a Double instance representing the specified double value. If a new Double instance is not required, this method should generally be used in preference to the constructor Double(double), as this method is likely to yield significantly better space and time performance by caching frequently requested values.


Float的valueOf(float)的官方文档介绍:

Returns a  Float  instance representing the specified  float  value. If a new  Float  instance is not required, this method should generally be used in preference to the constructor  Float(float) , as this method is likely to yield significantly better space and time performance by caching frequently requested values.


PS:运算符会触发拆箱,equals不会触发,‘==’ 也不会触发。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值