JDK源码学习之一lang包

JAVA编程思想,第一章,对对象的概念,以及访问权限的有了深入理解


JDK,看到Math
Character类的of方法,这里用到了二分查找法
public static UnicodeBlock of(int codePoint) {
            if (!isValidCodePoint(codePoint)) {
                throw new IllegalArgumentException();
            }

            int top, bottom, current;
            bottom = 0;
            top = blockStarts.length;
            current = top/2;

            // invariant: top > current >= bottom && codePoint >= unicodeBlockStarts[bottom]
            while (top - bottom > 1) {
                if (codePoint >= blockStarts[current]) {
                    bottom = current;
                } else {
                    top = current;
                }
                current = (top + bottom) / 2;
            }
            return blocks[current];
        }



Double类的compare方法,这里这个写法比较优雅
 
return (thisBits == anotherBits ?  0 : // Values are equal
                (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
                 1));                          // (0.0, -0.0) or (NaN, !NaN)

以及Integer里面的一些位移运算符(<<,>>,>>>等)

你可能感兴趣的:(JDK源码学习)