JDK源码解读(第九弹:Integer之其他常用方法)

之前我们已经详细解读了Integer的基本属性,toString方法,toUnsignedString0方法,parseInt方法,valueOf方法,接下来就再来看一下看其他几个比较常用也比较简单的方法。

compareTo和compare
这里看一下两个方法:

    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类实现了Comparable接口,compareTo方法是接口方法的实现,具体就是调用了compare方法。
compare方法代码简单明了,x小于y则返回-1,相等则返回0,否则返回1。

hashCode

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

    public static int hashCode(int value) {
        return value;
    }

这个方法就是直接返回对应的int值。

equals

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

先判断是不是Integer类型再比较值是不是相等。

decode

    public static Integer decode(String nm) throws NumberFormatException {
        int radix = 10;
        int index = 0;
        boolean negative = false;
        Integer result;

        if (nm.length() == 0)
            throw new NumberFormatException("Zero length string");
        char firstChar = nm.charAt(0);
        // Handle sign, if present
        if (firstChar == '-') {
            negative = true;
            index++;
        } else if (firstChar == '+')
            index++;

        // Handle radix specifier, if present
        if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
            index += 2;
            radix = 16;
        }
        else if (nm.startsWith("#", index)) {
            index ++;
            radix = 16;
        }
        else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
            index ++;
            radix = 8;
        }

        if (nm.startsWith("-", index) || nm.startsWith("+", index))
            throw new NumberFormatException("Sign character in wrong position");

        try {
            result = Integer.valueOf(nm.substring(index), radix);
            result = negative ? Integer.valueOf(-result.intValue()) : result;
        } catch (NumberFormatException e) {
            // If number is Integer.MIN_VALUE, we'll end up here. The next line
            // handles this case, and causes any genuine format error to be
            // rethrown.
            String constant = negative ? ("-" + nm.substring(index))
                                       : nm.substring(index);
            result = Integer.valueOf(constant, radix);
        }
        return result;
    }

decode方法主要作用是把带前缀的字符串转成Integer型,
0x0X#前缀的解析为十六进制进行转换,0前缀的解析为八进制进行转换,没有前缀的则解析为十进制进行转换。
比如Integer.decode("15")的结果为15,Integer.decode("0x15")Integer.decode("#15")结果都为21,Integer.decode("015")结果为13。

byteValue, shortValue, intValue, longValue, floatValue, doubleValue

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

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

    public int intValue() {
        return value;
    }

    public long longValue() {
        return (long)value;
    }

    public float floatValue() {
        return (float)value;
    }

    public double doubleValue() {
        return (double)value;
    }

这几个方法也是特别简单,其实就是转换成对应的类型进行返回。

还有几个方法,如bitCounthighestOneBitlowestOneBitnumberOfLeadingZerosnumberOfTrailingZerosreversereverseBytes等,这些方法的实现都是大量的移位操作、位与、位或等位运算,非常复杂,而且JDK基于高效的考虑,很多逻辑进行了转换,为了速度放弃了可读性,导致很难读懂,加之这几个方法也不太常用,这里就先不一一去解读。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值