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)的官方文档介绍:
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的官方文档介绍:
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 aFloat
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不会触发,‘==’ 也不会触发。
本文详细介绍了Java中基本数据类型与引用数据类型之间的转换,包括装箱与拆箱的过程及其实现方式。特别强调了Integer等包装类的valueOf方法在缓存方面的优化策略。
774

被折叠的 条评论
为什么被折叠?



