Integer 类在对象中包装了一个基本类型 int 的值。Integer 类型的对象包含一个 int 类型的字段。此外,该类提供了多个方法,能在int类型和 String类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法。
1.父类
接口
Integer的父类是Number类,Number类有一个java.io.Serializable序列化接口。
成员变量
private static final long serialVersionUID = -8742448824652078965L;
成员方法
- public abstract int intValue(); 抽象方法,将数据转化位int型并返回。
public abstract long longValue(); 抽象方法,将数据转化位long型并返回。
public abstract float floatValue(); 抽象方法,将数据转化位float型并返回。
public abstract double doubleValue(); 抽象方法,将数据转化位double型并返回。
public byte byteValue() 将数据转化位byte型并返回。
public short shortValue() 将数据转化位short型并返回。
2.接口
Integer拥有Comparable<Integer> 接口,该接口定义类的自然顺序,实现该接口的类就可以按这种方式排序,一般情况下如果某类对象自身具有可比较的特性就可以实现该接口,比如这里的Integer代表的是一种数,而数本身就具有比较的特性,就可以实现该接口。
3.成员变量和方法
成员变量
- @Native public static final int MIN_VALUE = 0x80000000; 定义Integer的最小值
- @Native public static final int MAX_VALUE = 0x7fffffff; 定义Integer的最大值
public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int"); 基本类型Integer的示例,即Integer.class。
final static char[] digits digits存储0到9和a到z共三十六个字符,因为Integer可以实现36进制,需要三十六个字符。
final static char [] DigitTens 主要用于获取0到99之间某个数的十位。
final static char [] DigitOnes 主要用于获取0到99之间某个数的个位。
final static int [] sizeTable 主要用作判断数长度的依据。
private final int value Integer类的数值。
@Native public static final int SIZE = 32 表示一个int型数据占32位存储空间。
public static final int BYTES = SIZE / Byte.SIZE; 表示一个int型数据占多少字节的存储空间。
成员方法:
public static String toString(int i, int radix) 该方法不是Object的toString方法。其作用就是将int类型的数字,转换为指定进制的数的字符串形式。radix在2到36范围内,因为Integer只能实现2到36进制的转换,如果是10进制就直接调用父类的toString方法输出为字符串。其他则通过十进制转换。转换时无论正数还是负数都当作正数,转换完后再根据其正负性加上符号。
public static String toHexString(int i) 转16进制字符串
public static String toOctalString(int i) 转8进制字符串
public static String toBinaryString(int i) 转2进制字符串
public static String toUnsignedString(int i, int radix) 上面三个方法都通过调用该方法实现进制转换,只是传入参数不同。该方法计算数据的有效二进制长度,再计算转换后需要多少位存储数据,然后新建一个合适的数组,再通过formatUnsignedInt方法实现进制转换。
static int formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) 进制转换的核心方法,通过位运算实现2,8,16进制的转换。
public static String toString(int i) 将数字转成字符串形式
public static String toUnsignedString(int i) 以无符号十进制值的形式返回参数的字符串表示形式
static void getChars(int i, int index, char[] buf) 该方法的作用就是将一个int类型的数按顺序放到char数组中。
static int stringSize(int x) 通过使用sizeTable数组巧妙判断数字的长度。
public static int parseInt(String s) throws NumberFormatException
public static int parseInt(String s, int radix) throws NumberFormatException 上一方法调用了该方法,默认传入的字符串时十进制的。此方法先判断字符串是否符合转换的标准,即是否是空串或者在不可转换的范围之内,是的话就抛出异常。数字默认为无符号数,通过一个布尔型变量标记正负,最后返回时再改变数据的正负性。此方法将字符转换成数字,将其他进制转换成十进制。
public static int parseUnsignedInt(String s) throws NumberFormatException
public static int parseUnsignedInt(String s, int radix) throws NumberFormatException 上一方法和此方法将目标数转换为无符号表示的long或是字符串,即二进制中的高位的第一位不当作符号位。
public static Integer valueOf(int i) 返回一个表示指定的 int 值的 Integer 对象。
public static Integer valueOf(String s) throws NumberFormatException 调用valueOf(String s, int radix)方法返回一个 Integer 对象,该对象中存储了String转换的radix进制数值,默认radix值为10。
public static Integer valueOf(String s, int radix) throws NumberFormatException 返回一个 Integer 对象,该对象中存储了String转换的radix进制数值。
private static class IntegerCache 缓冲池机制,作用是当数据范围在byte内时,所有该值的对象都是同一个对象。比如说课上举的Integer例子,当数据为3时,==操作是true;当数据为220时,==操作是false。
public Integer(int value) 指定数值的构造函数。
public Integer(String s) throws NumberFormatException 构造函数,将字符串转换成10进制作为对象的值。
public byte byteValue() 将Integer的值转换成byte类型,并返回。类似的还有shortValue(),intValue(),longValue(),floatValue(),doubleValue()。
public String toString() 继承的方法,将数值转换成字符串返回。
public int hashCode() 获取对象的hashcode。
public static int hashCode(int value) 返回原有的值。
public boolean equals(Object obj) 判断两个对象的值是否相等。
public static Integer getInteger(String nm) 从系统属性中查找数据然后转换为对应的Integer对象,如果系统中不存在待查找的属性,则返回null。
public static Integer getInteger(String nm, int val) 同上。
public static Integer decode(String nm) throws NumberFormatException 用来解析2进制,8进制,10进制16进制表示的字符串。
public int compareTo(Integer anotherInteger) 调用compare(int x, int y)方法比较两个值的大小
public static int compare(int x, int y) 比较两个值的大小,x>y,返回1,x = y返回0,x<y返回-1。
public static int compareUnsigned(int x, int y) 比较两个无符号数,实际操作是x,y各加上一个int的最小值后,调用compare(int x, int y) 进行比较。
public static long toUnsignedLong(int x) 将数据转换为long型,多余位用0占位。
public static int divideUnsigned(int dividend, int divisor) 将两个数转换为无符号的数,然后相除。
public static int remainderUnsigned(int dividend, int divisor) 将两个数转换为无符号的数,然后求余。
public static int highestOneBit(int i) 保留数据二进制形式最左边的1,其余位补0,并返回这个值的10进制。
public static int lowestOneBit(int i) 保留数据二进制形式最左边的1,其余位补0,并返回这个值的10进制。
public static int numberOfLeadingZeros(int i) 获取数据二进制形式左边有多少个0。
public static int numberOfTrailingZeros(int i) 获取数据二进制形式右边有多少个0。
public static int bitCount(int i) 获取数据二进制形式有多少个1。
public static int rotateLeft(int i, int distance) 循环左移。
public static int rotateRight(int i, int distance) 循环右移。
public static int reverse(int i) 把数据二进制形式的高位和低位对应位置数据互换。
public static int signum(int i) 返回数据的正负性,0时则返回0,正数返回1,负数返回-1。
public static int reverseBytes(int i) 数据二进制形式最高8位与低8位互换,中间的两个8位互换。
public static int max(int a, int b) 返回a,b中的较大值。
public static int min(int a, int b) 返回a,b中的较小值。