先看下Integer的類聲明:
同樣地,Integer是一個不變類,實作了Comparable接口,繼承了Number類(見下圖)。
除此之外,Integer類還有三點需要說的。
1. 靜态内部類IntegerCache
private static class IntegerCache {
static final int low = -;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
/**預設的緩存的最大的數*/
int h = ;
/**從配置中讀取*/
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
/**要比127大*/
i = Math.max(i, );
// Maximum array size is Integer.MAX_VALUE
/**但是不能過大*/
h = Math.min(i, Integer.MAX_VALUE - (-low) -);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
/**将[low, high]區間的值緩存起來,預設緩存區間[-128, 127]*/
cache = new Integer[(high - low) + ];
int j = low;
for(int k = ; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= ;
}
private IntegerCache() {}
}
來看一個例子:
Integer i1 = ;
Integer i2 = ;
Integer i3 = ;
Integer i4 = ;
Integer i5 = Integer.valueOf();
System.out.println(i1 == i2);
System.out.println(i3 == i4);
System.out.println(i1 == i5);
輸出:
true
false
true
也就是說i1,i2與i5都使用的是緩存的Integer對象,而i3和i4使用的是新建立的Integer對象。請看
Integer valueOf(int i)
方法:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
2. int parseInt(String s, int radix) 和 String toString(int i, int radix)
首先,一看參數名就知道可以進行進制轉換。其次,radix的範圍隻能是[2, 36]。36即10個數字加26個字母,不區分大小寫。
然後對于parseInt方法來說,一看傳回類型是int,也就意味着所處理的數字大小隻能在int範圍内,即[-2147483648, 2147483647]。超出範圍就會抛出異常
java.lang.NumberFormatException
。
使用這兩個函數就可以将一個數字從任意進制[2, 36]轉到另一進制。當然以int為橋梁,自然也會受到int範圍的限制。
請看:
String unknown = "7fffffff";
int temp;
System.out.println((temp=Integer.parseInt(unknown, )));
System.out.println(Integer.toString(temp, ));
輸出結果:
當
unknown = "-80000000"
時,
-
-
注意此處必須添符号,否則就會當成正數而越界。
3. 一些其他有趣的方法
int a = ;
System.out.println("original number:\t"+ a);
System.out.println("lowestOneBit:\t" + Integer.lowestOneBit(a));//最低一位是1的數
System.out.println("toBinaryString:\t" + Integer.toBinaryString(a));
System.out.println("highestOneBit:\t" + Integer.highestOneBit(a));//最高一位是1的數
System.out.println("numberOfLeadingZeros:\t" + Integer.numberOfLeadingZeros(a));
System.out.println("numberOfTrailingZeros:\t" + Integer.numberOfTrailingZeros(a));
System.out.println("bitCount:\t" + Integer.bitCount(a));//二進制中1的個數
System.out.println("rotateLeft:\t" + Integer.toBinaryString(Integer.rotateLeft(a, )));//旋轉,都是針對二進制字元串來做的
System.out.println("rotateRight:\t" + Integer.toBinaryString(Integer.rotateRight(a, )));
System.out.println("reverse:\t" + Integer.toBinaryString(Integer.reverse(a)));
System.out.println("reverseBytes:\t" + Integer.toBinaryString(Integer.reverseBytes(a)));
System.out.println("signum:\t" + Integer.signum(a));//符号,正負或零
結果
original number:
lowestOneBit:
toBinaryString:
highestOneBit:
numberOfLeadingZeros:
numberOfTrailingZeros:
bitCount:
rotateLeft:
rotateRight:
reverse:
reverseBytes:
signum: