【JAVA源码分析——Java.lang】包装类源码分析

包装类分析

Java 基本类型包括八种:byte、short、int、long、float、double、char、boolean

Number类型

其中数字类型包括:
整数型:
byte : -127~128

short : -32767~32768

int : 0x80000000~0x7fffffff

long : 0x8000000000000000L~0x7fffffffffffffffL

浮点型:
float : 0x0.000002P-126f / 0x1.fffffeP+127f

double : 0x0.0000000000001P-1022 ~ 0x1.fffffffffffffP+1023

java.lang.Number是数字类型对象的抽象类,子类包括Byte、Short、Integer、Long、Float、Double。还有BigDecimal和BigInteger

//定义非抽象方法byteValue 和shortValue
public byte byteValue() {
    return (byte)intValue();
}

public short shortValue() {
    return (short)intValue();
}

Number类定义方法如下
《【JAVA源码分析——Java.lang】包装类源码分析》

Byte

Byte中主要提供的方法有

  • 构造方法
  • toString
  • decode 解码方法,包括二进制、八进制、十六进制 静态工厂方法
  • compare 比较大小 静态工厂方法
  • compareTo 比较大小
  • valueOf 转换为Byte类型 静态工厂方法
  • parseByte 转换为Byte类型
  • intValue 类型转换 等类型转换方法

、、、decode、valueOf、parse
属性域

//最小值
public static final byte   MIN_VALUE = -128;

//最大值
public static final byte   MAX_VALUE = 127;

//声明类型
//获取JVM本地类型
public static final Class<Byte>     TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");

//二进制长度
public static final int SIZE = 8;

//byte存储
 private final byte value;

//静态内部类
private static class ByteCache {
    //不提供实例化方法
    private ByteCache(){}

    //缓存byte的所有值
    //好处显而易见,使用比较多时节省内存空间
    static final Byte cache[] = new Byte[-(-128) + 127 + 1];

    static {
        for(int i = 0; i < cache.length; i++)
            cache[i] = new Byte((byte)(i - 128));
    }
}

主要方法

//主要调用了Integer的parseInt方法
public static byte parseByte(String s, int radix)
    throws NumberFormatException {
    int i = Integer.parseInt(s, radix);
    if (i < MIN_VALUE || i > MAX_VALUE)
        throw new NumberFormatException(
            "Value out of range. Value:\"" + s + "\" Radix:" + radix);
    return (byte)i;
}
//调用了Integer.decode 八进制、十六进制转换
 public static Byte decode(String nm) throws NumberFormatException {
    int i = Integer.decode(nm);
    if (i < MIN_VALUE || i > MAX_VALUE)
        throw new NumberFormatException(
                "Value " + i + " out of range from input " + nm);
    return valueOf((byte)i);
}
//转换其他类型,直接进行强制转换,低级变量向高级变量转换
public double doubleValue() {
    return (double)value;
}

parseType 与 valueOf方法的区别

//返回值是byte,相当于在栈中划分新的内存空间
public static byte parseByte(String s, int radix)
    throws NumberFormatException {
    int i = Integer.parseInt(s, radix);
    if (i < MIN_VALUE || i > MAX_VALUE)
        throw new NumberFormatException(
            "Value out of range. Value:\"" + s + "\" Radix:" + radix);
    return (byte)i;
}

//静态工厂方法,返回包装类,实际存储内容已经缓存在静态内部类中
public static Byte valueOf(String s, int radix)
    throws NumberFormatException {
    return valueOf(parseByte(s, radix));
}

Short
short属性域与Btye类似的,所差别的主要是值域范围
short的主要方法也类似于btye,调用的是Integer的方法

Integer
Integer静态域类似于Byte的静态域,包括范围和type。
Byte和Short的toString 都是调用Integer进行实现的。

//提供的进制转换支持字符,根据设定,最高支持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'
};

//设置进制的toString
 public static String toString(int i, int radix) {
    //判断是否越界
    if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
        radix = 10;

    //若为10进制,调用toString(int i)
    if (radix == 10) {
        return toString(i);
    }

    char buf[] = new char[33];
    boolean negative = (i < 0);
    int charPos = 32;

    //判断正负
    if (!negative) {
        i = -i;
    }

    //辗转相除,存储到buff里
    while (i <= -radix) {
        buf[charPos--] = digits[-(i % radix)];
        i = i / radix;
    }
    buf[charPos] = digits[-i];

    if (negative) {
        buf[--charPos] = '-';
    }

    //从数组尾部存储,创建String调用String的截取创建
    return new String(buf, charPos, (33 - charPos));
}

//十进制String转换
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);
}

//定义范围数组,确定String长度
final static int [] sizeTable = { 9, 99, 999, 9999, 99999,999999, 9999999,99999999, 999999999, Integer.MAX_VALUE };

// 计算输入值位数
static int stringSize(int x) {
    for (int i=0; ; i++)
        if (x <= sizeTable[i])
            return i+1;
}

看到了Integer的getChars方法,开始没怎么看明白,附知乎大神解释
java源码中Integer.class中有个getChars方法,里面有个52429是怎么确定的?

    原文作者:寸辰
    原文地址: https://blog.csdn.net/cun_chen/article/details/51758974
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞