Java8 基础数据类型包装类-Integer

Java8 基础数据类型包装类-Integer


基础

//final修饰不可更改,每次赋值都是新建类(其中-128~127/jvm设置最大值,是直接从IntegerCache中获取的不是新建,可以通过==比较是否相同,其他数字是通过new新建的,不能使用==比较是否相同,因为是不同的对象,地址不一样,需用equals比较)
public final class Integer extends Number implements Comparable<Integer> {}

常量

@Native public static final int   MIN_VALUE = 0x80000000;
@Native public static final int   MAX_VALUE = 0x7fffffff;
//所有36进制的值
final static char[] digits = {
        '0' , '1' , '2' , '3' , '4' , '5' ,
        '6' , '7' , '8' , '9' , 'a' , 'b' ,
        'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
        'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
        'o' , 'p' , 'q' , 'r' , 's' , 't' ,
        'u' , 'v' , 'w' , 'x' , 'y' , 'z'
};
//32位
@Native public static final int SIZE = 32;
//字节数4
public static final int BYTES = SIZE / Byte.SIZE;

继承
抽象类Number
获取包装类与基本类型之间的转换值,short、int、long、byte、float、double。
实现
Comparable 接口
实现compareTo(T o)方法
数值范围
-2^31~2^31-1
最小值:0x80000000
最大值:0x7fffffff
私有静态内部类

//默认大小是-128~127,但也可以通过设置jvm启动参数来设置最大值。
private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        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) {
                try {
                    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);
                } 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() {}
    }

方法

转化成特定的进制

//i:传入的int型数值
//radix:需要转换的进制(2~36 不在此范围的按10进制处理)
public static String toString(int i, int radix){}
//转化成10进制字符串
public static String toString(int i) {}
//转化成10进制字符串
public String toString() {}

转化成无符号特定的进制值,即值得第一位不表示正负,表示为值

//i:传入的int型数值
//radix:需要转换的进制(2~36 不在此范围的按10进制处理)
public static String toUnsignedString(int i, int radix) {}
//转化成16进制无符号字符串
public static String toHexString(int i) {}
//转化成10进制无符号字符串
public static String toUnsignedString(int i) {}
//转化成8进制无符号字符串
public static String toOctalString(int i) {}
//转化成2进制无符号字符串
public static String toBinaryString(int i) {}

字符串转int

//s:数字型字符串
//radix:表示s字符串是几进制(radix不在2~36则抛异常)
public static int parseInt(String s, int radix)
                throws NumberFormatException{}
//字符串为十进制
public static int parseInt(String s) throws NumberFormatException {}
//无符号字符串转int(字符串第一位为“-”抛异常)
//radix:表示s字符串是几进制(radix不在2~36则抛异常)
public static int parseUnsignedInt(String s, int radix) throws NumberFormatException {}
//10进制无符号字符串转int(字符串第一位为“-”抛异常)
public static int parseUnsignedInt(String s) throws NumberFormatException {}

字符串、int转Integer

//s:数字型字符串
//radix:表示s字符串是几进制(radix不在2~36则抛异常)
public static Integer valueOf(String s, int radix) throws NumberFormatException {}
//字符串转10进制Integer
public static Integer valueOf(String s) throws NumberFormatException {}
//int转Integer
//其中-128~127的Integer是程序运行时就已经保存到IntegerCache中,所以在这个范围的数据是直接去的IntegerCache中的值
public static Integer valueOf(int i) {}
//nm:可以是010(8进制)、0x10(16进制)(#开头的在这个方法中也表示16进制)
public static Integer decode(String nm) throws NumberFormatException {}

Integer转基本数据类型

public byte byteValue() {}
public short shortValue() {}
public int intValue() {}
public long longValue() {}
//无符号x转long
public static long toUnsignedLong(int x) {
public float floatValue() {}
public double doubleValue() {}

获取系统属性的值

//nm:系统属性的名称
//val:如果为空时的默认值
public static Integer getInteger(String nm, int val) {}
public static Integer getInteger(String nm, Integer val) {}
//返回默认值为null
public static Integer getInteger(String nm) {}

比较及求和

public int compareTo(Integer anotherInteger) {}
//返回-1,1,0(-1表示y大,1表示x大)
public static int compare(int x, int y) {}
//比较无符号位数值大小
public static int compareUnsigned(int x, int y) {}
public static int sum(int a, int b) {}
public static int max(int a, int b) {}
public static int min(int a, int b) {}

无符号整数运算

//将dividend和divisor转成无符号整数相除取整
public static int divideUnsigned(int dividend, int divisor) {}
//将dividend和divisor转成无符号整数相除取余
public static int remainderUnsigned(int dividend, int divisor) {}


位运算

//如果一个数是0, 则返回0;
//如果是负数, 则返回 -2147483648:
//如果是正数, 返回的则是跟它最靠近的比它小的2的N次方
public static int highestOneBit(int i) {}
//返回其二进制最低位1的权值。
//以12为例,二进制为1100
//i:0 0 ... 0 1 1 0 0
//-i: 1 1 ... 1 0 1 0 0
//i & -i:0 0 ... 0 0 1 0 0
//返回值为4
public static int lowestOneBit(int i) {}
//返回这个数据的二进制串中从最左边算起连续的“0”的总数量。
public static int numberOfLeadingZeros(int i) {}
//返回这个数据的二进制串中从最右边算起连续的“0”的总数量。
public static int numberOfTrailingZeros(int i) {}
//给定一个int类型数据,返回这个数据的二进制串中“1”的总数量。
public static int bitCount(int i) {}
//循环左移
public static int rotateLeft(int i, int distance) {}
//循环右移
public static int rotateRight(int i, int distance) {}
//按位倒过来,反转二进制的顺序(1100->0011000...)
public static int reverse(int i) {}
// 按字节倒过来
public static int reverseBytes(int i) {}
//返回指定int值的符号函数。1,-1,0 正数,负数,0
public static int signum(int i) {}

猜你喜欢

转载自blog.csdn.net/u012562117/article/details/79003163
今日推荐