Byte's source code exploration

Byte

非可变类  final class
实现对比接口 	Comparable
继承于数字类  Number

Member variables

	byte  MIN_VALUE  最小值
	byte   MAX_VALUE 最大值
	Class<Byte>    TYPE   类类型
	byte value  初始值
	int SIZE  bit位数
	int BYTES  字节数

Static inner class

static class ByteCache cache class

Byte cache[]  -128~128的范围

Static code block

static {
    
    
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Byte((byte)(i - 128));  循环初始化 -128 ~ 128
        }

Construction method

public Byte(String s) throws NumberFormatException {
    
    
        this.value = parseByte(s, 10);
    }

In fact, the internal method is to call Ingeter

Member method

String toString(byte b) Get the string form of the value

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

Ingeter's toString method is called internally

static Byte valueOf(byte b) returns the wrapper class object

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

Return directly from the cache array

byte parseByte(String s, int radix) string value conversion process Byte value

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;
    }
radix为可与String转换的最小基数
内部调用Integer的 parseInt方法 
判断越界

int hashCode(byte value) Get hash code

public static int hashCode(byte value) {
    
    
        return (int)value;   数值本身就是hash码
    }

int toUnsignedInt(byte x) converted to Int type

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

& 0xff is to ensure that the lower eight bits remain unchanged, and the other bits become 0.

If there is an explanation error, please leave a message and contact the author to delete it in time to avoid guiding errors.

Guess you like

Origin blog.csdn.net/weixin_43946878/article/details/107935770