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基于高效的考虑,很多逻辑进行了转换,为了速度放弃了可读性,导致很难读懂,加之这几个方法也不太常用,这里就先不一一去解读。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章