jdk源码之Long

66 阅读2分钟

Long Field

/**
 * 存Long的值
 */
private final long value;
/**
 * 占用比特位数
 * @since 1.5
 */
@Native public static final int SIZE = 64;

/**
 * 字节数
 * @since 1.8
 */
public static final int BYTES = SIZE / Byte.SIZE;
/**
 * 最小值 -2<sup>63</sup>.
 */
@Native public static final long MIN_VALUE = 0x8000000000000000L;

/**
 * 最大值 2<sup>63</sup>-1.
 */
@Native public static final long MAX_VALUE = 0x7fffffffffffffffL;
// 类型
public static final Class<Long>     TYPE = (Class<Long>) Class.getPrimitiveClass("long");

常用方法

parseLong方法

public static long parseLong(String s) throws NumberFormatException {
    // 调用parseLong(String,int)
    return parseLong(s, 10);
}
// radix 底数(基数)即进制位数
public static long parseLong(String s, int radix)
          throws NumberFormatException
{
    if (s == null) {
        throw new NumberFormatException("Cannot parse null string");
    }
    // 基数大于36或者小于2都抛出转换异常
    if (radix < Character.MIN_RADIX) {
        throw new NumberFormatException("radix " + radix +
                                        " less than Character.MIN_RADIX");
    }
    if (radix > Character.MAX_RADIX) {
        throw new NumberFormatException("radix " + radix +
                                        " greater than Character.MAX_RADIX");
    }

    boolean negative = false;
    int i = 0, len = s.length();
    // 这里使用负数计算
    long limit = -Long.MAX_VALUE;

    if (len > 0) {
        // 获取第一个字符
        char firstChar = s.charAt(0);
        // 判断是否是- +
        if (firstChar < '0') { // Possible leading "+" or "-"
            if (firstChar == '-') {
                negative = true;
                limit = Long.MIN_VALUE;
            } else if (firstChar != '+') {
                throw NumberFormatException.forInputString(s, radix);
            }

            if (len == 1) { // Cannot have lone "+" or "-"
                throw NumberFormatException.forInputString(s, radix);
            }
            i++;
        }
        long multmin = limit / radix;
        long result = 0;
        while (i < len) {
            // Accumulating negatively avoids surprises near MAX_VALUE
            int digit = Character.digit(s.charAt(i++),radix);
            // 得到的数字和result的结果不能大于limit / radix
            if (digit < 0 || result < multmin) {
                throw NumberFormatException.forInputString(s, radix);
            }
            result *= radix;
            if (result < limit + digit) {
                throw NumberFormatException.forInputString(s, radix);
            }
            result -= digit;
        }
        return negative ? result : -result;
    } else {
        throw NumberFormatException.forInputString(s, radix);
    }
}

toString方法

非静态方法toString()

public String toString() {
    return toString(value);
}
public static String toString(long i) {
    int size = stringSize(i);
    //是否开启UTF-16编码
    if (COMPACT_STRINGS) {
        byte[] buf = new byte[size];
        getChars(i, size, buf);
        return new String(buf, LATIN1);
    } else {
        byte[] buf = new byte[size * 2];
        StringUTF16.getChars(i, size, buf);
        return new String(buf, UTF16);
    }
}

bitCount方法

/**
 * 返回指定 long 值的二进制补码二进制表示形式中的 1 位数
 * @since 1.5
 */
 @IntrinsicCandidate
 public static int bitCount(long i) {
    // HD, Figure 5-2
    i = i - ((i >>> 1) & 0x5555555555555555L);
    i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L);
    i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL;
    i = i + (i >>> 8);
    i = i + (i >>> 16);
    i = i + (i >>> 32);
    return (int)i & 0x7f;
 }

getLong方法

getLong(String,long)若是系统变量没有配置值,则取值val

public static Long getLong(String nm, long val) {
    Long result = Long.getLong(nm, null);
    return (result == null) ? Long.valueOf(val) : result;
}
public static Long getLong(String nm, Long val) {
    String v = null;
    try {
        v = System.getProperty(nm);
    } catch (IllegalArgumentException | NullPointerException e) {
    }
    if (v != null) {
        try {
            return Long.decode(v);
        } catch (NumberFormatException e) {
        }
    }
    return val;
}

max方法

/**
 * 返回最大的一个值
 * @since 1.8
 */
public static long max(long a, long b) {
    return Math.max(a, b);
}
public static long max(long a, long b) {
    return (a >= b) ? a : b;
}

valueOf方法

值在-128到127之间用缓存的对象

@IntrinsicCandidate
public static Long valueOf(long l) {
    final int offset = 128;
    if (l >= -128 && l <= 127) { // will cache
        return LongCache.cache[(int)l + offset];
    }
    return new Long(l);
}

highestOneBit方法

public static long highestOneBit(long i) {
    return i & (MIN_VALUE >>> numberOfLeadingZeros(i));
}
@IntrinsicCandidate
public static int numberOfLeadingZeros(long i) {
    int x = (int)(i >>> 32);
    return x == 0 ? 32 + Integer.numberOfLeadingZeros((int)i)
            : Integer.numberOfLeadingZeros(x);
}

toBinaryString方法

public static String toBinaryString(long i) {
    return toUnsignedString0(i, 1);
}

toOctalString方法

public static String toOctalString(long i) {
    return toUnsignedString0(i, 3);
}

toHexString方法

public static String toHexString(long i) {
    return toUnsignedString0(i, 4);
}
static String toUnsignedString0(long val, int shift) {
    // assert shift > 0 && shift <=5 : "Illegal shift value";
    int mag = Long.SIZE - Long.numberOfLeadingZeros(val);
    int chars = Math.max(((mag + (shift - 1)) / shift), 1);
    if (COMPACT_STRINGS) {
        byte[] buf = new byte[chars];
        formatUnsignedLong0(val, shift, buf, 0, chars);
        return new String(buf, LATIN1);
    } else {
        byte[] buf = new byte[chars * 2];
        formatUnsignedLong0UTF16(val, shift, buf, 0, chars);
        return new String(buf, UTF16);
    }
}