Integer类源码浅析

1、首先Integer提供了两类工具类,包括把一个int类型转成二进等,

  其实执行转换算法只有一个方法:

 1     public static String toString(int i, int radix) {
 2 
 3         if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
 4             radix = 10;
 5 
 6         /* Use the faster version */
 7         if (radix == 10) {
 8             return toString(i);
 9         }
10 
11         char buf[] = new char[33];
12         boolean negative = (i < 0);
13         int charPos = 32;
14 
15         if (!negative) {
16             i = -i;
17         }
18 
19         while (i <= -radix) {
20             buf[charPos--] = digits[-(i % radix)];
21             i = i / radix;
22         }
23         buf[charPos] = digits[-i];
24 
25         if (negative) {
26             buf[--charPos] = '-';
27         }
28 
29         return new String(buf, charPos, (33 - charPos));
30     }

2、测试的示例代码

1 //定义一个Integer 变量
2 Integer i = 1;
3 Integer j = 1;
4 System.out.println(i==j); //true
5         
6 Integer x = 128;
7 Integer y = 128;
8 System.out.println(x==y); //false

为什么会出现这样的结果呢,因为Integer内部维护了一个缓存类IntegerCache,默认缓存-128~127的数据

 1 /**
 2      * Cache to support the object identity semantics of autoboxing for values between
 3      * -128 and 127 (inclusive) as required by JLS.
 4      *
 5      * The cache is initialized on first usage.  The size of the cache
 6      * may be controlled by the -XX:AutoBoxCacheMax=<size> option.
 7      * During VM initialization, java.lang.Integer.IntegerCache.high property
 8      * may be set and saved in the private system properties in the
 9      * sun.misc.VM class.
10      */

IntegerCache缓存类的大小是可以设置,通过 -XX:AutoBoxCacheMax=<size> 这个选项来设置

 1     private static class IntegerCache {
 2         static final int low = -128;
 3         static final int high;
 4         static final Integer cache[];
 5 
 6         static {
 7             // high value may be configured by property
 8             int h = 127;
 9             String integerCacheHighPropValue =
10                 sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
11             if (integerCacheHighPropValue != null) {
12                 int i = parseInt(integerCacheHighPropValue);
13                 i = Math.max(i, 127);
14                 // Maximum array size is Integer.MAX_VALUE
15                 h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
16             }
17             high = h;
18 
19             cache = new Integer[(high - low) + 1];
20             int j = low;
21             for(int k = 0; k < cache.length; k++)
22                 cache[k] = new Integer(j++);
23         }
24 
25         private IntegerCache() {}
26     }

3、Integer类的内部维护了一个int基本类型的变量

提供了两个构造方法来初始化,一个构造方法接受int类型,一个接受String类型,接受类型的构造方法,调用了静态方法parseInt(s,10);

4、还有JDK1.7新添加的一个方法

 1     /**
 2      * Compares two {@code int} values numerically.
 3      * The value returned is identical to what would be returned by:
 4      * <pre>
 5      *    Integer.valueOf(x).compareTo(Integer.valueOf(y))
 6      * </pre>
 7      *
 8      * @param  x the first {@code int} to compare
 9      * @param  y the second {@code int} to compare
10      * @return the value {@code 0} if {@code x == y};
11      *         a value less than {@code 0} if {@code x < y}; and
12      *         a value greater than {@code 0} if {@code x > y}
13      * @since 1.7
14      */
15     public static int compare(int x, int y) {
16         return (x < y) ? -1 : ((x == y) ? 0 : 1);
17     }

其他方法就不做分析,可以直接参看API

 

posted @ 2016-08-09 16:59  练子  阅读(450)  评论(0编辑  收藏  举报