Long 与Integer 是数值类型中使用频率最高的两个,也是提供支持方法最多的两个 他们提供出来的方法功能也是高度的相似
public final class Long extends Number implements Comparable<Long> {}
//值为 2^63-1 的常量,它表示 long 类型能够表示的最大值
@Native public static final long MIN_VALUE = 0x8000000000000000L;
//值为 -2^63 的常量,它表示 long 类型能够表示的最小值
@Native public static final long MAX_VALUE = 0x7fffffffffffffffL;
//用来以二进制补码形式表示 long 值的比特位数
@Native public static final int SIZE = 64;
//二进制补码形式表示 long 值的字节数
public static final int BYTES = SIZE / Byte.SIZE;
//表示基本类型 long 的 Class 实例
@SuppressWarnings("unchecked")
public static final Class<Long> TYPE = (Class<Long>) Class.getPrimitiveClass("long");
//构造一个新分配的Long对象,该对象表示指定的long参数。
public Long(long value) {
this.value = value;
}
//构造一个新分配的Long对象,该对象表示参数long指示的 String 的值。
public Long(String s) throws NumberFormatException {
this.value = parseLong(s, 10);
}
返回String
表示指定的对象 long
。
public static String toString(long i, int radix) {
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
radix = 10;
if (radix == 10)
return toString(i);
char[] buf = new char[65];
int charPos = 64;
boolean negative = (i < 0);
if (!negative) {
i = -i;
}
while (i <= -radix) {
buf[charPos--] = Integer.digits[(int)(-(i % radix))];
i = i / radix;
}
buf[charPos] = Integer.digits[(int)(-i)];
if (negative) {
buf[--charPos] = '-';
}
return new String(buf, charPos, (65 - charPos));
}
toBinaryString(long i):返回long`以2为底的无符号整数形式返回参数的字符串表示形式。
toHexString(long i): 返回long 以16为底的无符号整数形式返回参数的字符串表示形式。
toOctalString(long i):返回long以8为底的无符号整数形式的参数字符串表示形式。
toUnsignedString(long i): 以无符号十进制值形式返回参数的字符串表示形式。
toUnsignedString(long i, int radix): 返回第一个参数的字符串表示形式,作为第二个参数指定的基数中的无符号整数值。
将long值复制到目标字符数组。
static void getChars(long i, int index, char[] buf) {
long q;
int r;
int charPos = index;
char sign = 0;
if (i < 0) {
sign = '-';
i = -i;
}
// Get 2 digits/iteration using longs until quotient fits into an int
while (i > Integer.MAX_VALUE) {
q = i / 100;
// really: r = i - (q * 100);
r = (int)(i - ((q << 6) + (q << 5) + (q << 2)));
i = q;
buf[--charPos] = Integer.DigitOnes[r];
buf[--charPos] = Integer.DigitTens[r];
}
// Get 2 digits/iteration using ints
int q2;
int i2 = (int)i;
while (i2 >= 65536) {
q2 = i2 / 100;
// really: r = i2 - (q * 100);
r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2));
i2 = q2;
buf[--charPos] = Integer.DigitOnes[r];
buf[--charPos] = Integer.DigitTens[r];
}
// Fall thru to fast mode for smaller numbers
// assert(i2 <= 65536, i2);
for (;;) {
q2 = (i2 * 52429) >>> (16+3);
r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ...
buf[--charPos] = Integer.digits[r];
i2 = q2;
if (i2 == 0) break;
}
if (sign != 0) {
buf[--charPos] = sign;
}
}
//两个对象进行比较 ,实际比较两个对象的value值
//根本还是通过调用compare(long x, long y)
public int compareTo(Long anotherLong) {
return compare(this.value, anotherLong.value);
}
//三目表达式
public static int compare(long x, long y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
两个基本类型int 当做无符号数进行比较 通过+MIN_VALUE进行转换 根本还是调用static int compare(long x, long y)
public static int compareUnsigned(long x, long y) {
return compare(x + MIN_VALUE, y + MIN_VALUE);
}
剩下的方法和Integer雷同很多,以后再更
Long 与Integer 是数值类型中使用频率最高的两个,也是提供支持方法最多的两个
他们提供出来的方法功能也是高度的相似