接着讲,Integer类 继承Number超类,实现Comparable(Integer)接口,这一节,主要一起看看Integer类内部代码。
属性:
public static final int MAX_VALUE = 0x7FFFFFFF;
public static final int MIN_VALUE = 0x80000000;
public static final int SIZE = 32;
private final int value;
private static final byte[] NTZ_TABLE = {
32, 0, 1, 12, 2, 6, -1, 13, 3, -1, 7, -1, -1, -1, -1, 14,
10, 4, -1, -1, 8, -1, -1, 25, -1, -1, -1, -1, -1, 21, 27, 15,
31, 11, 5, -1, -1, -1, -1, -1, 9, -1, -1, 24, -1, -1, 20, 26,
30, -1, -1, -1, -1, 23, -1, 19, 29, -1, 22, 18, 28, 17, 16, -1
};
构造方法:
public Integer(int value) {
this.value = value;
}
public Integer(String string) throws NumberFormatException {
this(parseInt(string));
}
对象方法:
@Override
public byte byteValue() {
return (byte) value;
}
public int compareTo(Integer object) {
return compare(value, object.value);
}
@Override
public double doubleValue() {
return value;
}
@Override
public boolean equals(Object o) {
return (o instanceof Integer) && (((Integer) o).value == value);
}
@Override
public float floatValue() {
return value;
}
@Override
public int hashCode() {
return value;
}
@Override
public int intValue() {
return value;
}
@Override
public long longValue() {
return value;
}
@Override
public short shortValue() {
return (short) value;
}
@Override
public String toString() {
return Integer.toString(value);
}
类方法:
private static final Integer[] SMALL_VALUES = new Integer[256];
static {
for (int i = -128; i < 128; i++) {
SMALL_VALUES[i + 128] = new Integer(i);
}
}
public static Integer valueOf(int i) {
return i >= 128 || i < -128 ? new Integer(i) : SMALL_VALUES[i + 128];
}
public static int signum(int i) {
return (i >> 31) | (-i >>> 31); // Hacker's delight 2-7
}
public static int reverse(int i) {
// Hacker's Delight 7-1, with minor tweak from Veldmeijer
// http://graphics.stanford.edu/~seander/bithacks.html
i = ((i >>> 1) & 0x55555555) | ((i & 0x55555555) << 1);
i = ((i >>> 2) & 0x33333333) | ((i & 0x33333333) << 2);
i = ((i >>> 4) & 0x0F0F0F0F) | ((i & 0x0F0F0F0F) << 4);
i = ((i >>> 8) & 0x00FF00FF) | ((i & 0x00FF00FF) << 8);
return ((i >>> 16) ) | ((i ) << 16);
}
public static int reverseBytes(int i) {
// Hacker's Delight 7-1, with minor tweak from Veldmeijer
// http://graphics.stanford.edu/~seander/bithacks.html
i = ((i >>> 8) & 0x00FF00FF) | ((i & 0x00FF00FF) << 8);
return ( i >>> 16 ) | ( i << 16);
}
public static int rotateRight(int i, int distance) {
// Shift distances are mod 32 (JLS3 15.19), so we needn't mask -distance
return (i >>> distance) | (i << -distance);
}
public static int rotateLeft(int i, int distance) {
// Shift distances are mod 32 (JLS3 15.19), so we needn't mask -distance
return (i << distance) | (i >>> -distance);
}
public static int bitCount(int i) {
// Hacker's Delight, Figure 5-2
i -= (i >> 1) & 0x55555555;
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
i = ((i >> 4) + i) & 0x0F0F0F0F;
i += i >> 8;
i += i >> 16;
return i & 0x0000003F;
}
public static int numberOfTrailingZeros(int i) {
return NTZ_TABLE[((i & -i) * 0x0450FBAF) >>> 26];
}
public static int numberOfLeadingZeros(int i) {
// Hacker's Delight, Figure 5-6
if (i <= 0) {
return (~i >> 26) & 32;
}
int n = 1;
if (i >> 16 == 0) {
n += 16;
i <<= 16;
}
if (i >> 24 == 0) {
n += 8;
i <<= 8;
}
if (i >> 28 == 0) {
n += 4;
i <<= 4;
}
if (i >> 30 == 0) {
n += 2;
i <<= 2;
}
return n - (i >>> 31);
}
public static int lowestOneBit(int i) {
return i & -i;
}
public static int highestOneBit(int i) {
// Hacker's Delight, Figure 3-1
i |= (i >> 1);
i |= (i >> 2);
i |= (i >> 4);
i |= (i >> 8);
i |= (i >> 16);
return i - (i >>> 1);
}
public static Integer valueOf(String string, int radix) throws NumberFormatException {
return valueOf(parseInt(string, radix));
}
public static Integer valueOf(String string) throws NumberFormatException {
return valueOf(parseInt(string));
}
public static String toString(int i, int radix) {
return IntegralToString.intToString(i, radix);
}
public static String toString(int i) {
return IntegralToString.intToString(i);
}
public static String toOctalString(int i) {
return IntegralToString.intToOctalString(i);
}
public static String toHexString(int i) {
return IntegralToString.intToHexString(i, false, 0);
}
public static String toBinaryString(int i) {
return IntegralToString.intToBinaryString(i);
}
private static int parse(String string, int offset, int radix, boolean negative) throws NumberFormatException {
int max = Integer.MIN_VALUE / radix;
int result = 0;
int length = string.length();
while (offset < length) {
int digit = Character.digit(string.charAt(offset++), radix);
if (digit == -1) {
throw invalidInt(string);
}
if (max > result) {
throw invalidInt(string);
}
int next = result * radix - digit;
if (next > result) {
throw invalidInt(string);
}
result = next;
}
if (!negative) {
result = -result;
if (result < 0) {
throw invalidInt(string);
}
}
return result;
}
public static int parsePositiveInt(String string, int radix) throws NumberFormatException {
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
throw new NumberFormatException("Invalid radix: " + radix);
}
if (string == null || string.length() == 0) {
throw invalidInt(string);
}
return parse(string, 0, radix, false);
}
public static int parsePositiveInt(String string) throws NumberFormatException {
return parsePositiveInt(string, 10);
}
public static int parseInt(String string, int radix) throws NumberFormatException {
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
throw new NumberFormatException("Invalid radix: " + radix);
}
if (string == null || string.isEmpty()) {
throw invalidInt(string);
}
char firstChar = string.charAt(0);
int firstDigitIndex = (firstChar == '-' || firstChar == '+') ? 1 : 0;
if (firstDigitIndex == string.length()) {
throw invalidInt(string);
}
return parse(string, firstDigitIndex, radix, firstChar == '-');
}
public static int parseInt(String string) throws NumberFormatException {
return parseInt(string, 10);
}
public static Integer getInteger(String string, Integer defaultValue) {
if (string == null || string.length() == 0) {
return defaultValue;
}
String prop = System.getProperty(string);
if (prop == null) {
return defaultValue;
}
try {
return decode(prop);
} catch (NumberFormatException ex) {
return defaultValue;
}
}
public static Integer getInteger(String string, int defaultValue) {
if (string == null || string.length() == 0) {
return valueOf(defaultValue);
}
String prop = System.getProperty(string);
if (prop == null) {
return valueOf(defaultValue);
}
try {
return decode(prop);
} catch (NumberFormatException ex) {
return valueOf(defaultValue);
}
}
public static Integer getInteger(String string) {
if (string == null || string.length() == 0) {
return null;
}
String prop = System.getProperty(string);
if (prop == null) {
return null;
}
try {
return decode(prop);
} catch (NumberFormatException ex) {
return null;
}
}
public static Integer decode(String string) throws NumberFormatException {
int length = string.length();
if (length == 0) {
throw invalidInt(string);
}
int i = 0;
char firstDigit = string.charAt(i);
boolean negative = firstDigit == '-';
if (negative || firstDigit == '+') {
if (length == 1) {
throw invalidInt(string);
}
firstDigit = string.charAt(++i);
}
int base = 10;
if (firstDigit == '0') {
if (++i == length) {
return valueOf(0);
}
if ((firstDigit = string.charAt(i)) == 'x' || firstDigit == 'X') {
if (++i == length) {
throw invalidInt(string);
}
base = 16;
} else {
base = 8;
}
} else if (firstDigit == '#') {
if (++i == length) {
throw invalidInt(string);
}
base = 16;
}
int result = parse(string, i, base, negative);
return valueOf(result);
}
public static int compare(int lhs, int rhs) {
return lhs < rhs ? -1 : (lhs == rhs ? 0 : 1);
}
private static NumberFormatException invalidInt(String s) {
throw new NumberFormatException("Invalid int: \"" + s + "\"");
}
@SuppressWarnings("unchecked")
public static final Class<Integer> TYPE
= (Class<Integer>) int[].class.getComponentType();