探索JDK

探索JDK之Byte类

2018-10-29  本文已影响0人  苏小小北

1. 简介

Byte类封装了一个值byte原始类型的一个对象。一个Byte类型的对象包含一个字段的类型是byte。此外,该类提供了一些方法处理byte、String的相关的操作。

2. 源码


(1) 实例变量:

1.value:表示数值大小(final不可变)

    private final byte value;
(2) 实例方法:

1.byteValue:返回byte类型值

    public byte byteValue() {
        return value;
    }

2.shortValue:返回short类型值

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

3.intValue:返回int类型值

    public int intValue() {
        return (int)value;
    }

4.longValue:返回long类型值

    public long longValue() {
        return (long)value;
    }

5.floatValue:返回float类型值

    public float floatValue() {
        return (float)value;
    }

6.doubleValue:返回doubleValue类型值

    public double doubleValue() {
        return (double)value;
    }

7.toString:重写toString方法,先转换为int,然后调用Integer.toString(int)

    public String toString() {
        return Integer.toString((int)value);
    }

8.hashCode:重写hashCode,返回对应int类型的value

    public static int hashCode(byte value) {
        return (int)value;
    }

9.equals:比较大小

    public boolean equals(Object obj) {
        if (obj instanceof Byte) {
            return value == ((Byte)obj).byteValue();
        }
        return false;
    }

10.compareTo:比较当前Byte和另一Byte的数值差x-y

    public int compareTo(Byte anotherByte) {
        return compare(this.value, anotherByte.value);
    }
(3) 类变量:

1.MIN_VALUE:Byte最小值,为-128

    public static final byte   MIN_VALUE = -128;

2.MAX_VALUE:Byte最大值,为127

    public static final byte   MAX_VALUE = 127;

3.TYPE:对应的原始类的类型“byte”

public static final Class<Byte>     TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");

4.SIZE:Byte数值二进制占用bit数目,为8

    public static final int SIZE = 8;

5.BYTES:Byte数值二进制占用byte数目,为1

    public static final int BYTES = SIZE / Byte.SIZE;

6.serialVersionUID:序列版本号,用于序列化和反序列化

    private static final long serialVersionUID = -7183698231559129828L;
(4) 类方法:

1.toString(byte):重写toString方法,返回value
直接调用了Integer.parseInt(String, int)方法

    public static String toString(byte b) {
        return Integer.toString((int)s, 10);
    }

2.parseByte(String, int):解析String为byte,base为radix,(不是每次new,看缓存池是否存在)

    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;
    }

3.parseByte(String):解析String为byte,base为10,(不是每次new,看缓存池是否存在)

    public static byte parseByte(String s) throws NumberFormatException {
        return parseByte(s, 10);
    }

4.valueOf(String, int):同parseShort(String, int)

    public static Byte valueOf(String s, int radix)
        throws NumberFormatException {
        return valueOf(parseByte(s, radix));
    }

5.valueOf(String):同parseShort(String)

    public static Byte valueOf(String s) throws NumberFormatException {
        return valueOf(s, 10);
    }

6.valueOf(byte):返回byte值的Byte对象,(不是每次new,看缓存池是否存在)

    public static Byte valueOf(byte b) {
        final int offset = 128;
        return ByteCache.cache[(int)b + offset];
    }

7.decode(String):解析short类型的字符串,用于读取系统属性

    public static Short 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((short)i);
    }

8.hashCode():重写hashCode,返回value值

    public static int hashCode(short value) {
        return (int)value;
    }

9.compare(byte , byte ):返回x-y的差值,返回类型为int

    public static int compare(byte x, byte y) {
        return x - y;
    }

10.toUnsignedInt(byte)::二进制按无符号类型重写计算值,返回int类型

    public static int toUnsignedInt(byte x) {
        return ((int) x) & 0xff;
    }

11.toUnsignedLong(byte)::二进制按无符号类型重写计算值,返回long类型

    public static long toUnsignedLong(byte x) {
        return ((long) x) & 0xffL;
    }
(5) 构造器:

1.Byte(byte value) :

    public Byte(byte value) {
        this.value = value;
    }

2.Byte(String s):

    public Byte(String s) throws NumberFormatException {
        this.value = parseByte(s, 10);
    }
(6) 静态内部类:

1.ByteCache:Byte对象缓存池,缓存范围-128-127
与ByteCache不同的是,ByteCache大小和范围是固定的

    private static class ByteCache {
        private ByteCache(){}

        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));
        }
    }

其他


本人也是在慢慢学习中,如有错误还请原谅、敬请指出,谢谢!

上一篇下一篇

猜你喜欢

热点阅读