static { // high value may be configured by property int h = 127; String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) { int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1); } high = h;
cache = new Integer[(high - low) + 1];//大小为正负两边总的数量 int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); }
/** * Returns the absolute value of an {@code int} value. * If the argument is not negative, the argument is returned. * If the argument is negative, the negation of the argument is returned. * * <p>Note that if the argument is equal to the value of * {@link Integer#MIN_VALUE}, the most negative representable * {@code int} value, the result is that same value, which is * negative. * * @param a the argument whose absolute value is to be determined * @return the absolute value of the argument. */ publicstaticintabs(int a){ return (a < 0) ? -a : a; }
从上面的jdk源码注释中,可以看出,当 a的值为Integer#MIN_VALUE时,返回原值。原因是因为 int 值的范围是 -2^31 ~ 2^31-1,也就是说最小的负数,没有对应的正整数,所以jdk实现中只有原样返回了。
publicstaticintdivide(int p, int q, RoundingMode mode){ checkNotNull(mode); if (q == 0) { thrownew ArithmeticException("/ by zero"); // for GWT } int div = p / q; int rem = p - q * div; // equal to p % q
if (rem == 0) { return div; }
/* * signum 如果p,q都是整数或者负数的时候,这个值为1, and -1 otherwise. */ int signum = 1 | ((p ^ q) >> (Integer.SIZE - 1));// 根据pq来看两个正负属性 boolean increment; switch (mode) { case UNNECESSARY: checkRoundingUnnecessary(rem == 0); // fall through case DOWN: increment = false; break; case UP: increment = true; break; case CEILING: increment = signum > 0; break; case FLOOR: increment = signum < 0; break; case HALF_EVEN: case HALF_DOWN: case HALF_UP: int absRem = abs(rem); int cmpRemToHalfDivisor = absRem - (abs(q) - absRem); // subtracting two nonnegative ints can't overflow // cmpRemToHalfDivisor has the same sign as compare(abs(rem), abs(q) / 2). if (cmpRemToHalfDivisor == 0) { // exactly on the half mark increment = (mode == HALF_UP || (mode == HALF_EVEN & (div & 1) != 0)); } else { increment = cmpRemToHalfDivisor > 0; // closer to the UP value } break; default: thrownew AssertionError(); } return increment ? div + signum : div; }