java Integer 源码深入解析

5 篇文章 0 订阅

首先来看class类的注释介绍:

/**
 * Integer类对基础类型int类进行了包装。
 * Integer对象包含了一个类型为int的单一字段(字段名为value)。
 * 此外,该类还提供了几种转换方法。
 * int 转 String,String 转 int,
 * 以及其他处理int的一些常数和方法。
 * @since JDK1.0
 */
 

一:类定义

 

public final class Integer extends Number implements Comparable<java.lang.Integer>

从类定义中我们可以知道以下几点:

1、final修饰类,所以Integer类不能被继承

2、Integer继承了Number类,所以该类可以调用longValue、floatValue、doubleValue等系列方法返回对应的类型的值。

public abstract class Number implements java.io.Serializable {

    private static final long serialVersionUID = -8742448824652078965L;

    public abstract int intValue();

    public abstract long longValue();

    public abstract float floatValue();

    public abstract double doubleValue();

    public byte byteValue() {
        return (byte)intValue();
    }

    public short shortValue() {
        return (short)intValue();
    }
}

3、Integer类实现了Comparable接口(这个接口只有compareTo一个方法),所以可以用compareTo进行比较,并且Integer对象只能和Integer类型的对象进行比较,不能和其他类型比较(至少调用compareTo方法无法比较)。

public interface Comparable<T> {
    public int compareTo(T o);
}

二:属性

@Native private static final long serialVersionUID = 1360826667806852920L;

    /**
     * 计算机中32位int类型变量的范围,其中int类型是带符号整数,最高位为符号位。
     * int 最小值 -2的31次方
     */
    @Native
    public static final int MIN_VALUE = 0x80000000;

    /**
     * int 最大值 2的31次方减1
     */
    @Native
    public static final int MAX_VALUE = 0x7fffffff;

    /**
     * 用来以二进制补码形式表示 int 值的比特位数。
     */
    @Native public static final int SIZE = 32;

    /**
     * 用来以二进制补码形式表示 int 值的字节数。1.8以后才有
     */
    public static final int BYTES = SIZE / Byte.SIZE;

    /**
     * 这是一个私有属性,
     * value属性就是Integer对象中真正保存int值的。
     */
    private final int value;

需要注意的就是这个int型的value属性。value属性就是Integer对象中真正保存int值的。

 

三:构造方法

在Integer类中提供了两个构造函数,分别针对构造参数为基本类型int和引用类型String。
这两个方法都是给当前的实例的value属性赋值,参数为int类型的构造器直接将参数赋值给value属性,
参数为String是将parseInt(String s, int radix)方法的返回值赋值。

    /**
     * 构造函数一
     * 当我们使用new Integer(10)创建一个Integer对象的时候,就会用以下形式给value赋值。
     */
    public Integer(int value) {
        this.value = value;
    }
/**
     * 构造函数二
     * 当我们使用Integer a = new Integer("2")创建一个Integer对象的时候;
     * 它表示 String 参数所指示的 int 值。
     */
    public Integer(String s) throws NumberFormatException {
        //从构造方法中我们可以知道,初始化一个Integer对象的时候只能创建一个十进制的整数。
        this.value = parseInt(s, 10);
    }

四:面试题

public class Test {
    public static void main(String[] args) {
        Integer a = 100;
        Integer b = 100;

        Integer c = 200;
        Integer d = 200;

        System.out.println(a == b);//①
        System.out.println(c == d);//②
    }
}

代码①和②分别打印的结果是什么?

①TRUE ②FALSE

分析:JDK1.5之后,java提供了自动装箱和自动拆箱的功能。自动装箱也就是调用了Integer类的一个静态方法valueOf方法。自动装箱也就是调用了Integer类的一个静态方法valueOf方法,先看源码:

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

    public static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final java.lang.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);
                    h = Math.min(i, java.lang.Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

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

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

        private IntegerCache() {}
    }

源码中有一个IntegerCache,这一个私有的内部类。这个类缓存了-128到127之间数字的包装类。意思就是当你new 一个int型的整数,先去判断是否在【-128到127这个区间内,如果再就把缓存中的那个包装类返回,Integer a = 100和Integer b= 100,都在这个区间,所以它们返回的都是同一个缓冲对象,所以①处相等为TRUE。当不在-128到127这个区间内,就会去真正new一个新的,

Integer c = 200和Integer d = 200不在这个区间内,所以分别new了一个新的对象,所以②处不相等为FALSE。

 

 

 

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值