# 字段

# 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);
    }
}