Java 源码--Long

字段

MIN_VALUE

Long的最小值。

@Native public static final long MIN_VALUE = 0x8000000000000000L;

MAX_VALUE

Long的最大值。

@Native public static final long MAX_VALUE = 0x7fffffffffffffffL;

TYPE

类原始类型long的类实例。

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

value

Long的值。

private final long value;

SIZE

Long的位数。

@Native public static final int SIZE = 64;

BYTES

Long的字节数。

public static final int BYTES = SIZE / Byte.SIZE;

serialVersionUID

Long的序列版本号。

@Native private static final long serialVersionUID = 4290774380558885855L;

构造方法

Long(long value)

public Long(long value) {
    this.value = value;
}

Long(String s)

public Long(String s) throws NumberFormatException {
    this.value = parseLong(s, 10);
}

方法

大部分方法与Integer相似,可参见Integer源码解析。

toUnsignedString(long i, int radix)

将i转为以radix为基数的无符号String类型。

public static String toUnsignedString(long i, int radix) {
    if (i >= 0)
        return toString(i, radix);
    else {
        switch (radix) {
        case 2:
            return toBinaryString(i);

        case 4:
            return toUnsignedString0(i, 2);

        case 8:
            return toOctalString(i);

        case 10:
            // 相当于i/10
            long quot = (i >>> 1) / 5;
            // 取余
            long rem = i - quot * 10;
            return toString(quot) + rem;

        case 16:
            return toHexString(i);

        case 32:
            return toUnsignedString0(i, 5);

        default:
            return toUnsignedBigInteger(i).toString(radix);
        }
    }
}

toUnsignedBigInteger(long i)

将i转为无符号BigInteger类型。

private static BigInteger toUnsignedBigInteger(long i) {
    if (i >= 0L)
        return BigInteger.valueOf(i);
    else {
        // 分为两半
        int upper = (int) (i >>> 32);
        int lower = (int) i;

        // return (upper << 32) + lower
        return (BigInteger.valueOf(Integer.toUnsignedLong(upper))).shiftLeft(32).
            add(BigInteger.valueOf(Integer.toUnsignedLong(lower)));
    }
}

stringSize(long x)

返回x作为String类型的长度。Integer是维护了定义了一个sizeTable。

static int stringSize(long x) {
    long p = 10;
    for (int i=1; i<19; i++) {
        if (x < p)
            return i;
        p = 10*p;
    }
    return 19;
}

parseUnsignedLong(String s, int radix)

将字符串s以radix为基数转换为无符号长整型数。

public static long parseUnsignedLong(String s, int radix)
            throws NumberFormatException {
    if (s == null)  {
        throw new NumberFormatException("null");
    }

    int len = s.length();
    if (len > 0) {
        char firstChar = s.charAt(0);
        if (firstChar == '-') {
            throw new
                NumberFormatException(String.format("Illegal leading minus sign " +
                                                   "on unsigned string %s.", s));
        } else {
            if (len <= 12 || // Long.MAX_VALUE in Character.MAX_RADIX is 13 digits
                (radix == 10 && len <= 18) ) { // Long.MAX_VALUE in base 10 is 19 digits
                return parseLong(s, radix);
            }

            // No need for range checks on len due to testing above.
            // 拆开
            long first = parseLong(s.substring(0, len - 1), radix);
            int second = Character.digit(s.charAt(len - 1), radix);
            if (second < 0) {
                throw new NumberFormatException("Bad digit at end of " + s);
            }
            long result = first * radix + second;
            if (compareUnsigned(result, first) < 0) {
                throw new NumberFormatException(String.format("String value %s exceeds " +
                                                              "range of unsigned long.", s));
            }
            return result;
        }
    } else {
        throw NumberFormatException.forInputString(s);
    }
}

内部类

LongCache

由代码可知,Long缓存的范围是-128-127。

private static class LongCache {
    private LongCache(){}
    static final Long cache[] = new Long[-(-128) + 127 + 1];

    static {
        for(int i = 0; i < cache.length; i++)
            cache[i] = new Long(i - 128);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章