Integer 装箱 拆箱 比较
2017-02-27 17:10
267 查看
赋值操作
把Integer类型的赋值给int类型,调用intValue()方法进行拆箱赋值。
把int类型赋值给Integer,会调用valueOf()方法对int进行装箱赋值。
2.Integer与int类型的比较
先对Integer调用intValue()进行拆箱,然后进行值比较
比较操作
1.Integer之间的比较对象比较
2.int之间的比较
进行值比较
valueOf()方法
valueOf()方法/** * Returns an {@code Integer} instance representing the specified * {@code int} value. If a new {@code Integer} instance is not * required, this method should generally be used in preference to * the constructor {@link #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. * * @param i an {@code int} value. * @return an {@code Integer} instance representing {@code i}. * @since 1.5 */ public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
出于性能的考虑,Integer类会缓存-128~127范围内的值。
在使用valueOf时,会判断i是否在-128~127范围内,如果在范围内,则从数组缓存中去;否则,新建。
在两个Integer进行比较的时候,一定要注意使用了valueOf()方法.
相关文章推荐
- Integer自动装箱和拆箱和比较
- Java 中 Integer 包装器类比较相等问题(拆箱、装箱、享元)
- Integer的自动拆箱和自动装箱的陷阱(整型数-128到127的值比较问题)
- java中int和Integer的==比较和拆箱装箱
- Java中Integer与int类型的比较--装箱和拆箱
- Integer 装箱拆箱 比较
- Java中Integer与int类型的比较--装箱和拆箱
- 六种数值包装类==比较,手动装箱与拆箱
- 由自动装箱和拆箱引发我看Integer源码
- java自动装箱、自动拆箱和正常情况性能比较
- 巧记Integer int 自动装箱与拆箱
- 黑马成员---StringBuffer,StringBuilder,Integer,自动装箱和拆箱,正则表达式:匹配,切割,替换,查找
- Integer自动装箱拆箱bug,创建对象在-128到127
- java自动装箱、拆箱——Integer细节
- Integer与int的装箱(缓存)与拆箱
- 装箱和拆箱、类型比较
- 基本类型的装箱与拆箱的比较
- java基本类型和基本类型包装型比较时的自动拆箱和自动装箱
- 由自动装箱和拆箱引发我看Integer源码
- int Integer 装箱 拆箱 自动装箱 自动拆箱