Byte类


接着上一节讲Number的子类——Byte类。该类继承Number,实现Comparable(Byte)接口。
重点内容

  • 构造方法:
public Byte(byte value) {
        this.value = value;
    }

public Byte(String string) throws NumberFormatException {
        this(parseByte(string));
    }
  • 类方法:
public static final int SIZE = 8;
public static final byte MIN_VALUE = (byte) 0x80;
public static final byte MAX_VALUE = (byte) 0x7F;

@SuppressWarnings("unchecked")
public static final Class<Byte> TYPE
            = (Class<Byte>) byte[].class.getComponentType();

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

public static byte parseByte(String string, int radix) throws NumberFormatException {
        int intValue = Integer.parseInt(string, radix);
        byte result = (byte) intValue;
        if (result == intValue) {
            return result;
        }
        throw new NumberFormatException("Value out of range for byte: \"" + string + "\"");


 public static int compare(byte lhs, byte rhs) {
        return lhs > rhs ? 1 : (lhs < rhs ? -1 : 0);
    }

 public static String toHexString(byte b, boolean upperCase) {
        return IntegralToString.byteToHexString(b, upperCase);
    }

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

 public static Byte valueOf(String string) throws NumberFormatException {
        return valueOf(parseByte(string));
    }

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

 private static final Byte[] VALUES = new Byte[256];

 static {
        for (int i = -128; i < 128; i++) {
            VALUES[i + 128] = new Byte((byte) i);
        }
    }

 public static Byte valueOf(byte b) {
        return VALUES[b + 128];
    }
  • 对象方法:
 private final byte value;

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

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

@Override
@FindBugsSuppressWarnings("RC_REF_COMPARISON")
    public boolean equals(Object object) {
        return (object == this) || ((object instance of Byte) && (((Byte) object).value == value));
    }

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

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

@Override
public byte byteValue() {
        return value;
}

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

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值