您现在的位置是:首页 > 文章详情

java.lang.Integer源码精读(二)

日期:2018-09-06点击:324

JDK源码精读汇总帖

getInteger()

然后比较少用的方法getInteger,这个方法是用来返回系统属性(String nm)的整数值的,很容易理解。

public static Integer getInteger(String nm) { return getInteger(nm, null); } public static Integer getInteger(String nm, int val) { Integer result = getInteger(nm, null); return (result == null) ? Integer.valueOf(val) : result; } public static Integer getInteger(String nm, Integer val) { String v = null; try { v = System.getProperty(nm); } catch (IllegalArgumentException e) { } catch (NullPointerException e) { } if (v != null) { try { return Integer.decode(v); } catch (NumberFormatException e) { } } return val; } 

decode(String)

看到上面的方法调用了decode方法,这个方法与valueOf和parseInt的区别就是后者只能分析字符串中是纯数字的,而decode可以分析类似0xff这种。

/** * 8进:010=>分析后为 8 * 10进:10=>分析后为 10 * 16进:#10|0X10|0x10=>分析后是 16 * 而valueOf 只能分析纯数字的String * 注意:这里的nm已经对应的是上面的v了,不是系统属性的key了 */ 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); // 符号处理 if (firstChar == '-') { negative = true; index++; } else if (firstChar == '+') index++; // 进制处理 if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) { index += 2; radix = 16; } else if (nm.startsWith("#", index)) { index ++; radix = 16; } // 以0开头且长度不止1位的才当成8进制,否则可能就是一个0 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);// 截取标志位(0x等)之后的数字,如果是Integer.MIN_VALUE,将会被传入2147483648,会被valueOf抛出异常,所以需要在catch里再处理 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. // 补回'-'变成-2147483648 给Integer.valueOf再处理 String constant = negative ? ("-" + nm.substring(index)) : nm.substring(index); result = Integer.valueOf(constant, radix); } return result; } 

hashCode()

​ hashCode方法,直接返回value。

public int hashCode() { return value; } 

equals()

​ equals方法比较value的比较。

public boolean equals(Object obj) { if (obj instanceof Integer) { return value == ((Integer)obj).intValue(); } return false; } 

highestOneBit(int)

​ highestOneBit这个函数返回的是数字i转换成二进制后最高位1对应的数值(10000....),即2^n对应的数字

/** * 返回最高位的1,1后面全补0对应的数,其实就是返回2^n * 如果一个数是0, 则返回0; *如果是负数, 则返回 -2147483648:【1000,0000,0000,0000,0000,0000,0000,0000】(二进制表示的数); * 如果是正数, 返回的则是跟它最靠近的比它小的2的N次方 * 原理就是不停的右移再或运算,使得i最后变成从最高位1开始后面全是1,如11111...1 */ public static int highestOneBit(int i) { // i右移1位在与i进行或运算,保证最高位两位都是1:11... i |= (i >> 1); // 这步完成之后,最高位4位都是1,1111... i |= (i >> 2); // 这步完成之后,最高位8位都是1,11111111... i |= (i >> 4); i |= (i >> 8); i |= (i >> 16); // 111...-011...到最高位的1,对应1000... return i - (i >>> 1); } 

lowestOneBit()

​ 然后是lowestOneBit,得到i最右边的1对应的数值,2^n的值。

/** * 这个方法很好理解,只考虑最右边1与它右边的数,最右边1与左边的数无需考虑。假设i最右边是100...0,不管1右边几个零,看看-i,反码为011..1,补码为1000,则i & -i ,1左边的都会变成0,1右边变成了100...0,正好得到最低位的1 */ public static int lowestOneBit(int i) { // HD, Section 2-1 return i & -i; } 

numberOfLeadingZeros()

​ 然后讨论numberOfLeadingZeros,这个方法的作用是返回参数i转成二进制后左边一共有多少个0。

public static int numberOfLeadingZeros(int i) { // 如果i是零,直接返回有32,代表二进制里32位都是零,java里int是4字节 if (i == 0) return 32; // 后面的逻辑完全是二分法的思维,判断i的第一个最左边的1在哪个位置,在判断过程中顺便累加了从左边开始0的个数,思路:先看你最左边1在高16位还是低16位,然后再判断你在16位中的高8位还是低8位,以此类推 int n = 1;// 0个数的计数器 // i右移16位之后有两种情况: // 情况1:i==0;代表左边的1位于低16位,这样的话n+=16代表至少有16个零了。然后把i左移16位,这样情况1和情况2的后续处理方法就一致了。 // 情况2:i!=0;代表左边的1位于高16位,后续在高16位中继续判断 if (i >>> 16 == 0) { n += 16; i <<= 16; } // 判断是否在低8位,在的话,0的个数加8,然后左移8位,后续与在高8位的情况同样处理 if (i >>> 24 == 0) { n += 8; i <<= 8; } if (i >>> 28 == 0) { n += 4; i <<= 4; } if (i >>> 30 == 0) { n += 2; i <<= 2; } // 到这一步,所有情况都被当成是否在最高2位的情况处理了,这个时候n已经代表了0的个数 // 如果i>>>31为0,则在低1位,最左边还有1个0,正好是n初始化为1的理由 // 如果i>>>31为1,则在高1位,最左边没有0了,初始化n为1会导致多算1个0,n-1正好是0的个数 n -= i >>> 31; return n; } 

numberOfTrailingZeros()

​ numberOfTrailingZeros方法也是二分法的思想,返回最右边0的个数。

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); } 

转载请注明出处

java.lang.Integer源码精读(二)
地址:https://www.jianshu.com/p/dc50508937c6

原文链接:https://yq.aliyun.com/articles/638051
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章