Integer类分析(jdk8)

一、构造函数

1. Integer类继承Number类,实现Comparable接口,重写了compareTo()方法。

2. Integer最小值为-2147483648,最大值为2147483647,若数值超出这个范围会报错。

3. Integer变量的声明可以通过Integer i = new Integer(100)声明;也可以通过Integer i = 100声明,其实调用的是Intager.valueOf(100)方法。

4.compareTo()方法的实现是: 如果x>y,则返回-1;如果x==y,则返回0;如果x<y,则返回1。

public final class Integer extends Number implements Comparable<Integer> {
   @Native public static final int   MIN_VALUE = 0x80000000;  // -2147483648

   @Native public static final int   MAX_VALUE = 0x7fffffff;  // 2147483647

   @SuppressWarnings("unchecked")
   public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");

   final static char[] digits = {
        '0' , '1' , '2' , '3' , '4' , '5' ,
        '6' , '7' , '8' , '9' , 'a' , 'b' ,
        'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
        'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
        'o' , 'p' , 'q' , 'r' , 's' , 't' ,
        'u' , 'v' , 'w' , 'x' , 'y' , 'z'
    };

  private final int value;

  public Integer(int value) {
        this.value = value;
  }
  public Integer(String s) throws NumberFormatException {
        this.value = parseInt(s, 10);
  }

  public int compareTo(Integer anotherInteger) {
        return compare(this.value, anotherInteger.value);
   }

   public static int compare(int x, int y) {
        return (x < y) ? -1 : ((x == y) ? 0 : 1); 
   }
}

二、Integer缓存

1. Integer把[-128,127]的数字放入缓存,以下面代码为例

当使用equals()方法时,无论数字是几,都是true。

当使用new Integer(num)声明一个变量时, 用==比较永远是false,因为new申请的是两块不同的内存空间,==比较的是内存地址,所以肯定不同。

当使用直接赋值声明一个变量时,若变量范围在[-128,127]时,==比较为true,因为它们用的是同一块缓存;超出的话会重新new一个Ingeter()变量,地址就不同了,就是false。

Integer integer1 = new Integer(128);          
Integer integer2 = new Integer(128);
Integer a = 128;
Integer b = 128;
System.out.println(integer1 == integer2);      // -129:false  -128:false  127:false  128:false
System.out.println(integer1.equals(integer2)); // -129:true   -128:true   127:true   128:true
System.out.println(a == b);                    // -129:false  -128:true   127:true   128:false
System.out.println(a.equals(b));               // -129:true   -128:true   127:true   128 true

2.       * Integer   (-128~127)

         * Boolean   (all)
         * Byte      (all)
         * Character (<=127)
         * Short     (-128~127)
         * Long      (-128~127)
         * Float     (none)
         * Double    (none)
private static class IntegerCache {
        static final int low = -128; // -128
        static final int high;       // 127
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue);
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);

            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }

三、valueOf()

给变量直接赋值的时候(Integer i = 100),就是使用valyeOf()方法来实现的。

由源码可以看到,若i在缓存范围内,则返回缓存中的数值,否则的话返回一个新的变量。

 public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

四、equals()

把比较的变量转换为int进行比较

 public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }

五、hashCode()

可以看到这里的hashCode就是value本身

 @Override
    public int hashCode() {
        return Integer.hashCode(value);
    }
 public static int hashCode(int value) {
        return value;
    }

六、comareTo()

public int compareTo(Integer anotherInteger) {
        return compare(this.value, anotherInteger.value);
    }
 public static int compare(int x, int y) {
        return (x < y) ? -1 : ((x == y) ? 0 : 1);
    }

  

  

原文地址:https://www.cnblogs.com/wwzyy/p/10135268.html