黑马程序员技术交流社区

标题: 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.)


作者: bullfrog    时间: 2014-9-7 22:26
fantacyleo 发表于 2014-9-7 22:21
第一个构造函数,每次必然创建一个新的Integer对象。对于第二个valueOf方法,API文档里说得很清楚了,对-12 ...

原来如此,多谢。
作者: 谢建平    时间: 2014-9-8 04:15
学习了  
作者: 卖艺人    时间: 2014-9-8 11:14
学到了~
作者: 挥斥方遒    时间: 2014-9-8 12:30
第二个是直接调用类方法吧。第一个就得new个对象了。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2