标题: new Integer(0)和 Integer.valueOf(0)有什么区别? [打印本页] 作者: bullfrog 时间: 2014-9-7 21:31 标题: new Integer(0)和 Integer.valueOf(0)有什么区别? 本帖最后由 bullfrog 于 2014-9-14 10:54 编辑
..............作者: 夜半风 时间: 2014-9-7 22:20
不知道我说的对不对 如果要打印第一个r拆箱 而第二个是不用的 作者: fantacyleo 时间: 2014-9-7 22:21
第一个构造函数,每次必然创建一个新的Integer对象。对于第二个valueOf方法,API文档里说得很清楚了,对-128到+127之间的数(有时甚至还包括超出这个范围的数),会缓存起来,下次再通过valueOf创建这个数的Integer对象时,不会创建新对象,而是直接返回缓存的对象地址,也就是可以节约内存开销并提高运行效率( 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.)