1.包装类除了Void和Character,其他六个全部都继承自Number。Number是一个抽象类。如下:
public abstract class Number implements java.io.Serializable { public abstract int intValue(); public abstract long longValue(); public abstract float floatValue(); public abstract double doubleValue(); public byte byteValue() { return (byte)intValue(); } public short shortValue() { return (short)intValue(); } private static final long serialVersionUID = -8742448824652078965L; }
2.Integer、Byte、Short、Long内部都有"缓存"静态类,以Integer为例:
缓存即一个Integer数组,默认大小为-128~127。初始化时,会new出256个Integer对象存在此数组中。
private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; static { 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)); } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); } private IntegerCache() {} }
当调用Integer.valueOf方法时,会首先判断参数是否在缓存范围内,若在,直接返回缓存对象,否则,构造之。
public static Integer valueOf(int i) { assert IntegerCache.high >= 127; if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
可见,Integer将加载缓存数组延迟到了最晚,避免浪费空间。
另外可以通过intValue方法返回Integer对应的int值(其他包装类类似):
public int intValue() { return value; }
value是Integer成员变量,构造时被传递进来:
private final int value; public Integer(int value) { this.value = value; } public Integer(String s) throws NumberFormatException { this.value = parseInt(s, 10); }
3.除了Void类,其余包装类均实现了Comparable接口,可以通过compareTo方法比较两个包装类的大小。同时包装类提供了静态的compare方法比较两个参数的大小(以Integer为例):
public int compareTo(Integer anotherInteger) { return compare(this.value, anotherInteger.value); }
compareTo方法其实调用了compare静态方法:
public static int compare(int x, int y) { return (x < y) ? -1 : ((x == y) ? 0 : 1); }
4.Integer的getInteger方法的作用是返回具有指定名称的系统属性的整数值,不要误用。
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; }
5.包装类的toString方法经过了重写
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); } public String toString() { return toString(value); }
6.可以使用parseXXX方法将字符串转化为对应的数字。
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; } public static int parseInt(String s) throws NumberFormatException { return parseInt(s,10); }
【源码】java包装类总结
时间: 2025-01-13 04:17:49