---
title: "Java中new Integer() 和Integer.valueOf()二者效率的比较 "
author: "feikiss"
date: "2011-11-30T11:02:36Z"
tags: ["Java Integer valueOf JDK 源码"]
categories: ["编程语言"]
slug: "java-new-integer-integer-valueof"
draft: false
lastmod: "2012-02-01T11:23:25Z"
id: 1285283
url: "https://feikiss.iteye.com/blog/1285283"
---

最近在公司参与一个项目，项目挺大，耗时一年左右，具体业务就不说了。。。 8) 之后在项目开发将近结束时，公司用Coverity工具对整体代码进行了检视，结果发现了N多问题，好多都是自己不注意的细节，在感叹此工具的强大的同时，问了下项目经理这个工具的价格，告知，30万$ !!! 纳尼 :roll: 神马！尼玛 :evil: 代码检视这块儿和findbug也差不多啊， 这也忒狠了点吧。。。 8) 不扯淡了，步入正题。 在检视过程中，提到如下一个问题，就是在我们代码中用new Integer(a) 的地方，好多都提示说Ineffective way, use Integer.valueOf(int) intead. 一时感觉好奇，翻开源码查看，没有查出啥究竟，然后后来利用debug模式进入源码调试才发现，原来里面的实现也大有天地。。。 首先查看valueof(int) 方法的实现： \[code="java"] /\** * Returns a `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 * {@link #Integer(int)}, as this method is likely to yield * significantly better space and time performance by caching * frequently requested values. * * @param i an `int` value. * @return a `Integer` instance representing `i`. * @since 1.5 \*/ public static Integer valueOf(int i) { if(i &gt;= -128 && i &lt;= IntegerCache.high) return IntegerCache.cache\[i + 128]; else return new Integer(i); } \[/code] 注释里面说的很清楚，“如果一个Integer的实例不是必须的，那么此方法应该优先于构造器来使用。。。” :roll: 为什么呢？继续深入探究，进入IntegerCache类，这个是Integer类的一个内部私有类。 \[code="java"] private static class IntegerCache { static final int high; static final Integer cache\[]; static { final int low = -128; // high value may be configured by property int h = 127; if (integerCacheHighPropValue != null) { // Use Long.decode here to avoid invoking methods that // require Integer's autoboxing cache to be initialized int i = Long.decode(integerCacheHighPropValue).intValue(); i = Math.max(i, 127); // Maximum array size is Integer.MAX\_VALUE h = Math.min(i, Integer.MAX\_VALUE - -low); } high = h; cache = new Integer\[(high - low) + 1]; int j = low; for(int k = 0; k &lt; cache.length; k++) cache\[k] = new Integer(j++); } private IntegerCache() {} } \[/code] 至此，谜底基本解开，原来IntegerCache相当于做了一个缓存，在第一次被调用时，首先初始化生成了从-128到127共256个对象的数组，在以后凡是在这个范围内的int值都可以直接从此缓存中取，而不在再次生成Integer对象，大大提高了对象的利用率。 然后我做了一个测试类，来测试二者真正的效率区别： \[code="java"] public static void testEfficiency(){ int count = 10000000; long t1 = System.currentTimeMillis(); for(int i = 0; i &lt; count; i ++){ int a = new Integer(i%128 \*(i%2 == 0?-1:1)); } long t2 = System.currentTimeMillis(); for(int i = 0; i &lt; count; i ++){ int b = Integer.valueOf(i%128 \*(i%2 == 0?-1:1)); } long t3 = System.currentTimeMillis(); System.out.println("Time of new Integer() method:"+(t2-t1)+"ms"); System.out.println("Time of Integer.ValueOf:"+(t3-t2)+"ms"); } \[/code] 输出结果： \[code="java"] Time of new Integer() method:125ms Time of Integer.ValueOf:94ms\[/code] 二者似乎区别多大，不过这说明JDK虚拟机的效率比较高， :D 然后将这两种方式单独封装到两个方法中，利用两个程序进行调试，然后再看下javaw.exe所占的内存，区别就出来了。。。大家可以自己试一下，为了查看内存，可以将程序sleep 20s来查看， 我本机的测试结果： new Integer方式：13 516K Integer.valueOf方式：8 852K. 相差4664K. 实验基本到此结束，但是学习却只是一个开始，在实际项目中也可以参考此种实现方式，懒加载和缓存的思想。 :lol: