字段
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);
}
}