java.lang.Integer源码解析(JDK1.8)

一、概要

  • int的包装类,包含有int的字段
  • 提供了Integer和字符串之间转化的方法

二、实现接口或继承类
public final class Integer extends Number implements Comparable<Integer>

  • 继承Number抽象类,该类继承了java.io.Serializable类,该类有以下一些方法
    //以int形式返回指定数字的值,可能存在截断和舍入问题
    public abstract int intValue();…doubleValue
  • 实现Comparable接口,进行比较

三、常量

//Integer代表的值
private final int value;

//序列化ID,用于进行反序列化
@Native private static final long serialVersionUID = 1360826667806852920L;

//最小值-2^31
@Native public static final int   MIN_VALUE = 0x80000000;

//最大值2^31 - 1
@Native public static final int   MAX_VALUE = 0x7fffffff;

//int的class
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'
    };

四、构造器

    //创建实例
    //通常使用valueOf(int i)方法,因为此方法使用了缓存
    public Integer(int value) {
        this.value = value;
    }

五、方法

    //将int转化为String
    public static String toString(int i) {
        if (i == Integer.MIN_VALUE)
            return "-2147483648";
        //依次比较9,99,999...来判断参数i的位数
        int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
        char[] buf = new char[size];
        getChars(i, size, buf);
        return new String(buf, true);
    }
    
    //将String转化为10进制的数
    public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }
    
    //返回指定int值的Integer实例,此方法会使用缓存,大大提高了运行时间
    //IntegerCache.low和IntegerCache.high之间的数才存在缓存,
    //IntegerCache.low为-127,IntegerCache.high默认为128,也可以修改,
    //所以在这两者之间的数会直接从缓存中读取
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
    
    //返回long型
    public long longValue() {
        return (long)value;
    }
    
    //返回hash值
    @Override
    public int hashCode() {
        return Integer.hashCode(value);
    }
    
    //比较两个对象指定的值是否相等
    public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }
    
    //比较两个Integer
    public int compareTo(Integer anotherInteger) {
        return compare(this.value, anotherInteger.value);
    }
    //静态方法,比较两个int值
    public static int compare(int x, int y) {
        return (x < y) ? -1 : ((x == y) ? 0 : 1);
    }
    //求和
    public static int sum(int a, int b) {
        return a + b;
    }
    //求最大值
    public static int max(int a, int b) {
        return Math.max(a, b);
    }
    //最小值
    public static int min(int a, int b) {
        return Math.min(a, b);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值