jdk自学笔记:Byte源码分析

Byte是基本类型byte的包装类(byte是java的基本数据类型,存储整型数据,占据1个字节(8 bits),能够存储的数据范围是-128~+127)

   public final class Byte extends Number implements Comparable<Byte>
  • 最小值-128,最大值127
	public static final byte   MIN_VALUE = -128;

    public static final byte   MAX_VALUE = 127;
  • 构造函数
 public Byte(byte value) {
        this.value = value;
    }
 public Byte(String s) throws NumberFormatException {
        this.value = parseByte(s, 10);
    }
 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);
    }
  • toString:():Str
  public String toString() {
        return Integer.toString((int)value);
    }
  • hashCode():int
 @Override
    public int hashCode() {
        return Byte.hashCode(value);
    }
    public static int hashCode(byte value) {
        return (int)value;
    }
  • valueOf():
 	 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 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;
    }
  • equals(Object):boolean
  public boolean equals(Object obj) {
        if (obj instanceof Byte) {
            return value == ((Byte)obj).byteValue();
        }
        return false;
    }
    public byte byteValue() {
        return value;
    }
  • compareTo(Byte):int
 public int compareTo(Byte anotherByte) {
        return compare(this.value, anotherByte.value);
    }
 public static int compare(byte x, byte y) {
        return x - y;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值