Exploración del código fuente de Byte

Byte

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

Variables de miembro

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

Clase interna estática

clase estática ByteCache clase de caché

Byte cache[]  -128~128的范围

Bloque de código estático

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

Método de construcción

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

De hecho, el método interno es llamar a Ingeter

Método de miembro

String toString (byte b) Obtiene la forma de cadena del valor

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

El método toString de Ingeter se llama internamente

static Byte valueOf (byte b) devuelve el objeto de la clase contenedora

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

Regrese directamente desde la matriz de caché

byte parseByte (String s, int radix) proceso de conversión de valor de cadena Valor de 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;
    }
radix为可与String转换的最小基数
内部调用Integer的 parseInt方法 
判断越界

int hashCode (valor de byte) Obtener código hash

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

int toUnsignedInt (byte x) convertido a tipo Int

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

& 0xff es para asegurar que los ocho bits inferiores permanezcan sin cambios y los otros bits se conviertan en 0.

Si hay un error de explicación, deje un mensaje y comuníquese con el autor para eliminarlo a tiempo y evitar errores de guía.

Supongo que te gusta

Origin blog.csdn.net/weixin_43946878/article/details/107935770
Recomendado
Clasificación