深入学习java源码之ByteCache()与parseByte()

深入学习java源码之ByteCache()与parseByte()

Byte类在一个对象中包含一个基本类型byte的值。 类型为Byte的对象包含一个单一字段,其类型为byte 。 
此外,该类还提供了一些将byte转换为String和String转换为byte ,以及在处理byte时有用的其他常量和方法。 

方法

Modifier and Type Method and Description
byte byteValue()

Byte的值作为 byte

static int compare(byte x, byte y)

数值比较两个 byte值。

int compareTo(Byte anotherByte)

数字比较两个 Byte对象。

static Byte decode(String nm)

String解码成 Byte

double doubleValue()

返回此值 Bytedouble一个宽元转换后。

boolean equals(Object obj)

将此对象与指定的对象进行比较。

float floatValue()

返回此值 Bytefloat一个宽元转换后。

int hashCode()

返回这个Byte的哈希码; 等于调用intValue()的结果。

static int hashCode(byte value)

返回一个byte值的哈希码; 兼容Byte.hashCode()

int intValue()

返回此的值 Byte作为 int加宽原始转换之后。

long longValue()

返回此值 Bytelong一个宽元转换后。

static byte parseByte(String s)

将字符串参数解析为带符号的十进制 byte

static byte parseByte(String s, int radix)

将字符串参数解析为第二个参数指定的基数中的带符号的 byte

short shortValue()

返回此值 Byteshort一个宽元转换后。

String toString()

返回一个 String对象,代表这个 Byte的值。

static String toString(byte b)

返回一个新的 String对象,代表指定的 byte

static int toUnsignedInt(byte x)

的参数的转换 int由无符号转换。

static long toUnsignedLong(byte x)

参数给转换 long由无符号转换。

static Byte valueOf(byte b)

返回一个 Byte指定的 byte值的 Byte实例。

static Byte valueOf(String s)

返回一个 Byte物体保持由指定的给定的值 String

static Byte valueOf(String s, int radix)

返回一个 Byte对象,该对象保存从指定的String中 String的值,并使用第二个参数给出的基数进行解析。

java源码

package java.lang;

public final class Byte extends Number implements Comparable<Byte> {


    public static final byte   MIN_VALUE = -128;

    public static final byte   MAX_VALUE = 127;

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


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

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

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

    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 parseByte(String s) throws NumberFormatException {
        return parseByte(s, 10);
    }

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

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

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


    private final byte value;

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

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

    public byte byteValue() {
        return value;
    }

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

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

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

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

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

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

    @Override
    public int hashCode() {
        return Byte.hashCode(value);
    }

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

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

    public int compareTo(Byte anotherByte) {
        return compare(this.value, anotherByte.value);
    }

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

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

    public static long toUnsignedLong(byte x) {
        return ((long) x) & 0xffL;
    }

    public static final int SIZE = 8;


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

    /** use serialVersionUID from JDK 1.1. for interoperability */
    private static final long serialVersionUID = -7183698231559129828L;
}
package java.lang;

public abstract class Number implements java.io.Serializable {

    public abstract int intValue();


    public abstract long longValue();


    public abstract float floatValue();

    public abstract double doubleValue();

    public byte byteValue() {
        return (byte)intValue();
    }


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

    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = -8742448824652078965L;
}
package java.lang;
import java.util.*;


public interface Comparable<T> {

    public int compareTo(T o);
}

猜你喜欢

转载自blog.csdn.net/qq_35029061/article/details/85522035