java源码分析(9)-Byte

Byte

1.Byte

Byte为final修饰不能继承,实现了comparable接口,可用于比较,同时继承了Number类,需要实现数字类型转换的一系列方法

public final class Byte extends Number implements Comparable<Byte> {
 public static final byte   MIN_VALUE = -128;
 public static final byte   MAX_VALUE = 127;
  public static final int SIZE = 8;//Byte用八位表示,即一个字节
  public byte byteValue() {
<span style="white-space:pre">	</span>return value;
    }
    public short shortValue() {
<span style="white-space:pre">	</span>return (short)value;
    }
    public int intValue() {
<span style="white-space:pre">	</span>return (int)value;
    }
    public long longValue() {
<span style="white-space:pre">	</span>return (long)value;
    }
    public float floatValue() {
<span style="white-space:pre">	</span>return (float)value;
    }
    public double doubleValue() {
<span style="white-space:pre">	</span>return (double)value;
    }

2.构造器

只有以下两个构造器

public Byte(byte value) {
        this.value = value;//传入的必须是Byte类型的值
    }
public Byte(String s) throws NumberFormatException {
        this.value = parseByte(s, 10);//传入的必须是可以转换为数字的字符串
    }
3.缓存值

Byte的缓存值,-128~127总共256个值,在这个区间内,jvm会直接使用缓存值,当值超出这个区间时,会出现溢出现象,如128会等于-128,从最小值再次开始算值

 private static class ByteCache {
	private ByteCache(){}

	static final Byte cache[] = new Byte[-(-128) + 127 + 1];//缓存数组的长度为256
	static {
	    for(int i = 0; i < cache.length; i++)
		cache[i] = new Byte((byte)(i - 128));//将-128~127从小到大缓存
	}
    }
4.parseByte
 public static byte parseByte(String s) throws NumberFormatException {
	return parseByte(s, 10);
    }
    public static byte parseByte(String s, int radix)
	throws NumberFormatException {
	int i = Integer.parseInt(s, radix);//s需为-128~127的字符,不然下一步就会报错
	if (i < MIN_VALUE || i > MAX_VALUE)
	    throw new NumberFormatException(
                "Value out of range. Value:\"" + s + "\" Radix:" + radix);
	return (byte)i;
    }
5.decode

解码转码方法,将String转为Byte

public static Byte decode(String nm) throws NumberFormatException {
        int radix = 10;
        int index = 0;
        boolean negative = false;
        Byte result;
        if (nm.startsWith("-")) {
            negative = true;
            index++;
        }
        //判定16进制数
	if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
            index += 2;
            radix = 16;
	} else if (nm.startsWith("#", index)) {
	    index++;
            radix = 16;
	} else if (nm.startsWith("0", index) && nm.length() > 1 + index) {//判定8进制数
	    index++;
            radix = 8;
	}
        if (nm.startsWith("-", index))//位置不正确的负号
            throw new NumberFormatException("Negative sign in wrong position");
        try {
            result = Byte.valueOf(nm.substring(index), radix);
            result = negative ? new Byte((byte)-result.byteValue()) : result;
        } catch (NumberFormatException e) {
            String constant = negative ? new String("-" + nm.substring(index))
                                       : nm.substring(index);
            result = Byte.valueOf(constant, radix);
        }
        return result;
    }
6.toString,hashCode,equals

public String toString() {
	return String.valueOf((int)value);//toString方法返回的是ACCII码的编码,而不是值本身,例如a的toString为97
    }
    public int hashCode() {
	return (int)value;//hash值返回的是asc码的数字值
    }
    public boolean equals(Object obj) {
	if (obj instanceof Byte) {
	    return value == ((Byte)obj).byteValue();//重写equals方法,比较value地址值,与==功能相同
	}
	return false;
    }






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值