Long 與Integer 是數值類型中使用頻率最高的兩個,也是提供支持方法最多的兩個
他們提供出來的方法功能也是高度的相似
一、類定義
public final class Long extends Number implements Comparable<Long> {}
- 類被聲明為final的,表示不能被繼承;
- 繼承了Number抽象類,可以用于數字類型的一系列轉換;
- 實現了Comparable接口,強行對實現它的每個類的對象進行整體排序
二、成員變量
//值為 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);
}
四、常用方法
1、toString(long i, int radix)
返回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): 返回第一個參數的字符串表示形式,作為第二個參數指定的基數中的無符號整數值。
2、getChars(long i, int index, char[] buf)
將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 intwhile (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 intsint 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;}
}
3、compareTo
//兩個對象進行比較 ,實際比較兩個對象的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);
}
4、compareUnsigned(long x, long y)
兩個基本類型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 是數值類型中使用頻率最高的兩個,也是提供支持方法最多的兩個
他們提供出來的方法功能也是高度的相似