Java源码 —— Integer
Integer 类在对象中封装了一个基本类型 int 的值。
Integer 类型的对象包含一个 int 类型的字段。
此外,该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法。
一、Integer类图
二、单类拆解
2.1 Number
抽象类 Number 是 BigDecimal、BigInteger、Byte、Double、Float、Integer、Long 和 Short 类的超类,此外JDK1.8还扩
展出了一些累加器(扩展抽象类Striped64,并基于此类实现累加器)。
Number定义了一系列将表示的数值转换基本数值类型的方法,具体类型如下:
整数类型:
byte(8位,用于表示最小数据单位;数值范围在-128~127)、int(32位,最常用)、long(64位,常用,后缀L/l,为了区分字母l与数字1通常用大写L,后缀一般比较少用)、short(16位,很少用)
浮点类型:
double(64位,后最D/d)、float(32位,后缀F/f)
Number实现了两个数值转换的方法,分别是byteValue()、shortValue(),在方法体重调用intValue()强制转换,代码如下:public byte byteValue() { return (byte)intValue(); } public short shortValue() { return (short)intValue(); }
2.2 Serializable
接口Serializable是一个标识接口,用于标识一个类是否启用序列号功能。未实现此接口的类将无法使其任何状态序列化或反序列化。
2.3 Comparable
接口Comparable定义了一个public int compareTo(T o)方法,可用于比较或排序。实现Comparable接口的对象列表或数组,可以通过Collections.sort或Arrays.sort进行自动排序。实现Comparable接口的对象可以用作有序映射(SortMap)中的键或有序集合(SortedSet)中的元素。
排序代码// 来自Collections类的方法 public static <T extends Comparable<? super T>> void sort(List<T> list) { list.sort(null); } public static <T> void sort(List<T> list, Comparator<? super T> c) { list.sort(c); } // 来自List接口的方法 default void sort(Comparator<? super E> c) { Object[] a = this.toArray();// 将当前list对象转成数组 Arrays.sort(a, (Comparator) c);// 对数组进行排序 ListIterator<E> i = this.listIterator();// 获得当前list对象对应的List迭代器 for (Object e : a) {// 对做好排序的数组进行迭代 i.next();// 将迭代器的指标指向下一个元素的指针位置 i.set((E) e);// 替换list当前指针对应元素 } }
从上面代码可以看到,Collections中不管是哪个sort方法最后都是调用List对象本身的sort方法做排序。在List.sort中将list对象转成数组,并调用Arrays.sort排序,排序之后再将排序顺序的对象按照下标一一替换List中的元素。
2.4 IntegerCache
IntegerCache是Integer类内部封装的一个缓存池,用于缓存自动装箱时的对象。默认缓存值范围在-128到127之间的Integer对象。缓存池的大小可以通过-XX:AutoBoxCacheMax=<size>参数来控制缓存的最大值,最小值-128不可配置,即缓存池的区间范围为[-128, <size>]。除此之外还可以通过设置虚拟机的java.lang.Integer.IntegerCache.high属性来控制缓存池的上限。不管以哪种方式设置上限值,最终都将以java.lang.Integer.IntegerCache.high property的方式呈现,最终上限值大于等于127。IntegerCache源码如下:private static class IntegerCache { static final int low = -128;// 缓存池值下限 static final int high;// 缓存池值上限 static final Integer cache[];// 缓存Integer对象的数组 static { // high value may be configured by property int h = 127;// 缓存池上限默认127 String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");// 获取设置的上限值(不管通过哪种方式设置上限,最终都会以property的方式呈现) if (integerCacheHighPropValue != null) {// 获取缓存池上限值 try { int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127);// 缓存上限值最低不小于127 // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1); } catch( NumberFormatException nfe) { // If the property cannot be parsed into an int, ignore it. } } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127; } private IntegerCache() {} }
三、常用方法
3.1 byteValue()3.2 shortValue()3.3 intValue()3.4 longValue()3.5 floatValue()3.6 doubleValue()3.7 int parseInt(String s),将字符串转换成int(十进制数)public static int parseInt(String s) throws NumberFormatException { return parseInt(s,10); }
3.8 int max(int a, int b),两个数取最大值,jdk1.8,内部调用Math类方法public static int max(int a, int b) { return Math.max(a, b); }
3.9 int min(int a, int b),两个数取最小值,jdk1.8,内部调用Math类方法public static int min(int a, int b) { return Math.max(a, b); }
3.10 int sum(int a, int b),两数求和,jdk1.8public static int sum(int a, int b) { return a + b; }
3.11 Integer valueOf(String s),将字符串转换成int(十进制)public static Integer valueOf(String s) throws NumberFormatException { return Integer.valueOf(parseInt(s, 10)); }
3.12 Integer valueOf(int i),返回一个表示指定int值的Integer对象,如果指定值在缓存区间则从IntegerCache中取,否则new Integer(i)返回一个新的对象public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }