概述
java.lang.*中Integer 源代码详解
- 核心方法
- String toString(int i, int radix)
- String toUnsignedString(int i, int radix)
- String toHexString(int i)
- String toOctalString(int i)
- String toString(int i)
- String toUnsignedString(int i)
- int parseInt(String s, int radix)
- int parseUnsignedInt(String s, int radix)
- Integer valueOf(String s, int radix)
- public Integer(int value)
- int hashCode(int value)
- boolean equals(Object obj)
- Integer getInteger(String nm)
- Integer decode(String nm)
- int compareTo(Integer anotherInteger)
- int compareUnsigned(int x, int y)
- int divideUnsigned(int dividend, int divisor)
- int remainderUnsigned(int dividend, int divisor)
- int highestOneBit(int i)
- int lowestOneBit(int i)
- int numberOfLeadingZeros(int i)
- int numberOfTrailingZeros(int i)
- int bitCount(int i)
- int rotateLeft(int i, int distance)
- int rotateRight(int i, int distance)
- int reverse(int i)
- int signum(int i)
- int reverseBytes(int i)
学习Java的同学注意了!!!
学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:543120397 我们一起学Java!
核心方法
String toString(int i, int radix) 获取i的radix进制形式,最多36进制[OK]
String toUnsignedString(int i, int radix) 获取i的radix进制字符串形式,最多36进制(不带符号位)[OK]
String toHexString(int i) 获取16进制字符串结果[OK]
String toOctalString(int i) 获取8进制字符串结果[OK]
String toBinaryString(int i) 获取2进制字符串结果[OK]
String toString(int i) 获取Integer对象的字符串形式[OK]
String toUnsignedString(int i) 获取Integer对象的无符号字符串形式[OK]
int parseInt(String s, int radix) 将字符串按radix进制转为int数值[OK]
int parseUnsignedInt(String s, int radix) 按照radix转换无符号位字符串数值为int类型[OK]
Integer valueOf(String s, int radix) 将字符串s按照radix进制,转化为int类型数值[OK]
public Integer(int value) 构造方法[OK]
int hashCode(int value) 获取Integer对象的HashCode值[OK]
boolean equals(Object obj) 比较两个对象的值是否相等[OK]
Integer getInteger(String nm) 从System.properties中获取属性值,并转为int值[OK]
Integer decode(String nm) 方法解码字符串转换为整数[OK]
int compareTo(Integer anotherInteger) 比较两个Integer对象的值,如果相等返回0[OK]
int compareUnsigned(int x, int y) 比较两个 int值,以数值方式将值视为无符号[OK]
int divideUnsigned(int dividend, int divisor) 返回将第一个参数除以秒的无符号商,其中每个参数和结果被解释为无符号值[OK]
int remainderUnsigned(int dividend, int divisor) 返回无符号余数,将第一个参数除以秒,其中每个参数和结果被解释为无符号值[OK]
int highestOneBit(int i) 返回一个 int值与至多一个单个1位,在最高阶(“最左侧”)的位置在指定的一个位 int值[OK]
int lowestOneBit(int i) 在指定的 int值中,以最低位(最右边)为1位返回一个最多为单个1位的 int值[OK]
int numberOfLeadingZeros(int i) 返回的最高阶的(“最左边的”)中所指定的二进制补码表示的一个位前述零个比特的数量 int值[OK]
int numberOfTrailingZeros(int i) 返回零位以下最低阶(“最右边的”)的数量在指定的二进制补码表示的一个位 int值[OK]
int bitCount(int i) 返回指定的int值的二进制补码二进制表示中的 int数[OK]
int rotateLeft(int i, int distance) 返回通过旋转指定的二的补码的二进制表示而得到的值 int由位指定数目的左值[OK]
int rotateRight(int i, int distance) 返回通过旋转指定的二的补码的二进制表示而得到的值 int右移位的指定数值[OK]
int reverse(int i) 指定的二进制补码表示反转位的顺序而获得的值 int值[OK]
int signum(int i) 返回指定的int值的signum函数[OK]
int reverseBytes(int i) 返回反转指定的二进制补码表示的字节顺序而获得的值 int值[OK]
String toString(int i, int radix)
/**
* 获取i的radix进制字符串形式,最多36进制
* 样例:
* Integer.toString(-10,2)
* 输出结果:
* -1010
*/
public static String toString(int i, int radix) {
//如果radix不在[2,36]范围内时,radix默认为10进制
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
radix = 10;
/* Use the faster version */
if (radix == 10) {
return toString(i);
}
//int最大值的2进制,再加上符号位,总共33位
char buf[] = new char[33];
boolean negative = (i < 0);//判断是否为负数
int charPos = 32;
if (!negative) {//如果i为正数,将其转为负数进行进制转换
i = -i;
}
while (i <= -radix) {//直到i<=-radix
//将i对radix取余,去对应进制表的数组中寻找对应符号
//buf[charPos--]从后往前往buf数组中插入进制符号
buf[charPos--] = digits[-(i % radix)];
//将i的值除以radix取整,作为一次循环处理逻辑
i = i / radix;
}
//将最后剩下的i,取到对应的符号,塞到charPos位置
buf[charPos] = digits[-i];
if (negative) {//最后根据negative符号,拼接符号位
buf[--charPos] = '-';
}
//最后输出结果
return new String(buf, charPos, (33 - charPos));
}
String toUnsignedString(int i, int radix)
/**
* 获取i的radix进制字符串形式,最多36进制
* 样例:
* Integer.toUnsignedString(-0x800000,2)
* 输出结果:
* 11111111100000000000000000000000
*/
public static String toUnsignedString(int i, int radix) {
return Long.toUnsignedString(toUnsignedLong(i), radix);
}
public static long toUnsignedLong(int x) {
return ((long) x) & 0xffffffffL;
}
public static String toUnsignedString(long i, int radix) {
if (i >= 0)
return toString(i, radix);
else {
switch (radix) {
case 2:
return toBinaryString(i);
case 4:
return toUnsignedString0(i, 2);
case 8:
return toOctalString(i);
case 10:
/*
* We can get the effect of an unsigned division by 10
* on a long value by first shifting right, yielding a
* positive value, and then dividing by 5. This
* allows the last digit and preceding digits to be
* isolated more quickly than by an initial conversion
* to BigInteger.
*/
long quot = (i >>> 1) / 5;
long rem = i - quot * 10;
return toString(quot) + rem;
case 16:
return toHexString(i);
case 32:
return toUnsignedString0(i, 5);
default:
return toUnsignedBigInteger(i).toString(radix);
}
}
}
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));
}
String toHexString(int i)
public static String toHexString(int i) {
return toUnsignedString0(i, 4);
}
String toOctalString(int i)
public static String toOctalString(int i) {
return toUnsignedString0(i, 3);
}
String toString(int i)
/**
* 获取Integer对象的字符串形式
*/
public static String toString(int i) {
if (i == Integer.MIN_VALUE)
return "-2147483648";
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
char[] buf = new char[size];
getChars(i, size, buf);
return new String(buf, true);
}
String toUnsignedString(int i)
/**
* 获取Integer对象的无符号字符串形式
*/
public static String toUnsignedString(int i, int radix) {
return Long.toUnsignedString(toUnsignedLong(i), radix);
}
public static String toUnsignedString(long i, int radix) {
if (i >= 0)
return toString(i, radix);
else {
switch (radix) {
case 2:
return toBinaryString(i);
case 4:
return toUnsignedString0(i, 2);
case 8:
return toOctalString(i);
case 10:
/*
* We can get the effect of an unsigned division by 10
* on a long value by first shifting right, yielding a
* positive value, and then dividing by 5. This
* allows the last digit and preceding digits to be
* isolated more quickly than by an initial conversion
* to BigInteger.
*/
long quot = (i >>> 1) / 5;
long rem = i - quot * 10;
return toString(quot) + rem;
case 16:
return toHexString(i);
case 32:
return toUnsignedString0(i, 5);
default:
return toUnsignedBigInteger(i).toString(radix);
}
}
int parseInt(String s, int radix)
/**
* 将字符串按radix进制转为int数值
* 默认是10进制
*/
public static int parseInt(String s, int radix)
throws NumberFormatException
{
/*
* WARNING: This method may be invoked early during VM initialization
* before IntegerCache is initialized. Care must be taken to not use
* the valueOf method.
*/
if (s == null) {
throw new NumberFormatException("null");
}
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");
}
int result = 0;
boolean negative = false;
int i = 0, len = s.length();
int limit = -Integer.MAX_VALUE;
int multmin;
int digit;
if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
negative = true;
limit = Integer.MIN_VALUE;
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s);
if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
multmin = limit / radix;
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
return negative ? result : -result;
}
int parseUnsignedInt(String s, int radix)
/**
* 按照radix转换无符号位字符串数值为int类型
*/
public static int parseUnsignedInt(String s) throws NumberFormatException {
return parseUnsignedInt(s, 10);
}
public static int parseUnsignedInt(String s, int radix)
throws NumberFormatException {
if (s == null) {//判断s是否为null
throw new NumberFormatException("null");
}
int len = s.length();
if (len > 0) {
//获取字符串首号位置的字符
char firstChar = s.charAt(0);
//如果是以-开头,则抛出异常
if (firstChar == '-') {
throw new
NumberFormatException(String.format("Illegal leading minus sign " +
"on unsigned string %s.", s));
} else {
//按照36进制,int类型的值最多存储5位
if (len <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits
(radix == 10 && len <= 9) ) { // Integer.MAX_VALUE in base 10 is 10 digits
return parseInt(s, radix);
} else {
long ell = Long.parseLong(s, radix);
if ((ell & 0xffff_ffff_0000_0000L) == 0) {
return (int) ell;
} else {
throw new
NumberFormatException(String.format("String value %s exceeds " +
"range of unsigned int.", s));
}
}
}
} else {
throw NumberFormatException.forInputString(s);
}
}
Integer valueOf(String s, int radix)
/**
* 将字符串s按照radix进制,转化为int类型数值
* 底层调用的是parseInt方法
*/
public static Integer valueOf(String s, int radix) throws NumberFormatException {
return Integer.valueOf(parseInt(s,radix));
}
public Integer(int value)
public Integer(int value) {
this.value = value;
}
public Integer(String s) throws NumberFormatException {
this.value = parseInt(s, 10);
}
int hashCode(int value)
/**
* 获取Integer对象的hashCode值
*/
public int hashCode() {
return Integer.hashCode(value);
}
public static int hashCode(int value) {
return value;
}
boolean equals(Object obj)
public boolean equals(Object obj) {
//首先判断obj是否为Integer类型
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
Integer getInteger(String nm)
/**
* 从System.properties中获取属性值,并转为int值
*/
public static Integer getInteger(String nm) {
return getInteger(nm, null);
}
public static Integer getInteger(String nm, Integer val) {
String v = null;
try {
v = System.getProperty(nm);
} catch (IllegalArgumentException | NullPointerException e) {
}
if (v != null) {
try {
return Integer.decode(v);
} catch (NumberFormatException e) {
}
}
return val;
}
Integer decode(String nm)
/**
* 方法解码字符串转换为整数
* 它接受十进制,十六进制和八进制数
* 样例:
* System.out.println(Integer.decode("#20"));
System.out.println(Integer.decode("0x20"));
System.out.println(Integer.decode("0X20"));
System.out.println(Integer.decode("020"));
输出结果:
32
32
32
16
*/
public static Integer decode(String nm) throws NumberFormatException {
int radix = 10;
int index = 0;
boolean negative = false;
Integer result;
if (nm.length() == 0)
throw new NumberFormatException("Zero length string");
char firstChar = nm.charAt(0);
// Handle sign, if present
if (firstChar == '-') {
negative = true;
index++;
} else if (firstChar == '+')
index++;
// Handle radix specifier, if present
if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
index += 2;
radix = 16;
}
else if (nm.startsWith("#", index)) {
index ++;
radix = 16;
}
else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
index ++;
radix = 8;
}
if (nm.startsWith("-", index) || nm.startsWith("+", index))
throw new NumberFormatException("Sign character in wrong position");
try {
result = Integer.valueOf(nm.substring(index), radix);
result = negative ? Integer.valueOf(-result.intValue()) : result;
} catch (NumberFormatException e) {
// If number is Integer.MIN_VALUE, we'll end up here. The next line
// handles this case, and causes any genuine format error to be
// rethrown.
String constant = negative ? ("-" + nm.substring(index))
: nm.substring(index);
result = Integer.valueOf(constant, radix);
}
return result;
}
int compareTo(Integer anotherInteger)
/**
* 比较两个Integer对象的值
* 如果相等返回0
* 如果不相等返回-1或者1
*/
public int compareTo(Integer anotherInteger) {
return compare(this.value, anotherInteger.value);
}
public static int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
int compareUnsigned(int x, int y)
/**
* 比较两个int值,以数值方式将值视为无符号
*/
public static int compareUnsigned(int x, int y) {
return compare(x + MIN_VALUE, y + MIN_VALUE);
}
int divideUnsigned(int dividend, int divisor)
/**
* 返回将第一个参数除以秒的无符号商,其中每个参数和结果被解释为无符号值
*/
public static int divideUnsigned(int dividend, int divisor) {
// In lieu of tricky code, for now just use long arithmetic.
return (int)(toUnsignedLong(dividend) / toUnsignedLong(divisor));
}
int remainderUnsigned(int dividend, int divisor)
/**
* 返回无符号余数,将第一个参数除以秒,其中每个参数和结果被解释为无符号值
*/
public static int remainderUnsigned(int dividend, int divisor) {
// In lieu of tricky code, for now just use long arithmetic.
return (int)(toUnsignedLong(dividend) % toUnsignedLong(divisor));
}
int highestOneBit(int i)
/**
* 返回一个 int值与至多一个单个1位,在最高阶(“最左侧”)的位置在指定的一个位 int值
*/
public static int highestOneBit(int i) {
// HD, Figure 3-1
i |= (i >> 1);
i |= (i >> 2);
i |= (i >> 4);
i |= (i >> 8);
i |= (i >> 16);
return i - (i >>> 1);
}
int lowestOneBit(int i)
/**
* 在指定的 int值中,以最低位(最右边)为1位返回一个最多为单个1位的 int值
*/
public static int lowestOneBit(int i) {
// HD, Section 2-1
return i & -i;
}
int numberOfLeadingZeros(int i)
/**
* 返回的最高阶的(“最左边的”)中所指定的二进制补码表示的一个位前述零个比特的数量 int值
*/
public static int numberOfLeadingZeros(int i) {
// HD, Figure 5-6
if (i == 0)
return 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; }
n -= i >>> 31;
return n;
}
int numberOfTrailingZeros(int i)
/**
* 返回零位以下最低阶(“最右边的”)的数量在指定的二进制补码表示的一个位 int值
*/
public static int numberOfTrailingZeros(int i) {
// HD, Figure 5-14
int y;
if (i == 0) return 32;
int n = 31;
y = i <<16; if (y != 0) { n = n -16; i = y; }
y = i << 8; if (y != 0) { n = n - 8; i = y; }
y = i << 4; if (y != 0) { n = n - 4; i = y; }
y = i << 2; if (y != 0) { n = n - 2; i = y; }
return n - ((i << 1) >>> 31);
}
int bitCount(int i)
/**
* 返回指定的int值的二进制补码二进制表示中的 int数
*/
public static int bitCount(int i) {
// HD, Figure 5-2
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
int rotateLeft(int i, int distance)
/**
* 返回通过旋转指定的二的补码的二进制表示而得到的值 int由位指定数目的左值
*/
public static int rotateLeft(int i, int distance) {
return (i << distance) | (i >>> -distance);
}
int rotateRight(int i, int distance)
/**
* 返回通过旋转指定的二的补码的二进制表示而得到的值 int右移位的指定数值
*/
public static int rotateRight(int i, int distance) {
return (i >>> distance) | (i << -distance);
}
int reverse(int i)
/**
* 指定的二进制补码表示反转位的顺序而获得的值 int值
*/
public static int reverse(int i) {
// HD, Figure 7-1
i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555;
i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333;
i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f;
i = (i << 24) | ((i & 0xff00) << 8) |
((i >>> 8) & 0xff00) | (i >>> 24);
return i;
}
int signum(int i)
/**
* 返回指定的int值的signum函数
* 如果指定的值为负,返回值为-1;如果指定的值为零,返回值为0;如果指定的值为正,返回值为1
*/
public static int signum(int i) {
// HD, Section 2-7
return (i >> 31) | (-i >>> 31);
}
int reverseBytes(int i)
/*
* 返回反转指定的二进制补码表示的字节顺序而获得的值 int值
*/
public static int reverseBytes(int i) {
return ((i >>> 24) ) |
((i >> 8) & 0xFF00) |
((i << 8) & 0xFF0000) |
((i << 24));
}
最后
以上就是着急衬衫为你收集整理的java.lang.*中Integer 源代码详解的全部内容,希望文章能够帮你解决java.lang.*中Integer 源代码详解所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复