Byte源码浅知浅写

 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.

package java.lang;

/**
 *
 * The {@code Byte} class wraps a value of primitive type {@code byte}
 * in an object.  An object of type {@code Byte} contains a single
 * field whose type is {@code byte}.
 *
 * <p>In addition, this class provides several methods for converting
 * a {@code byte} to a {@code String} and a {@code String} to a {@code
 * byte}, as well as other constants and methods useful when dealing
 * with a {@code byte}.
 *
 * @author  Nakul Saraiya
 * @author  Joseph D. Darcy
 * @see     java.lang.Number
 * @since   JDK1.1
 */
public final class Byte extends Number implements Comparable<Byte> {

// final表示Byte不可继承

   // 对于byte最小值的规定
    public static final byte   MIN_VALUE = -128;

  // 最小值
    public static final byte   MAX_VALUE = 127;

   // 警告静默 获取byte的原始类
    @SuppressWarnings("unchecked")
    public static final Class<Byte>     TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");

    // 将byte转为String 十位限制
    public static String toString(byte b) {
        return Integer.toString((int)b, 10);
    }

 

// 通过内部类创建一个具有全部的byte对象的数组,用于后面减少内存消耗

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

    // 数组的0位置是-128 所以想从数组中获取到对应的对象必须要加128来确定位置

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

  // 通过Integer的转换方法 将radix位的s转为int 当没有超出范围就转成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;
    }

   // 调用自身方法 默认十位
    public static byte parseByte(String s) throws NumberFormatException {
        return parseByte(s, 10);
    }

   // 先将转换成Byte 再放入valueOf中转化成取得数组中的对象
    public static Byte valueOf(String s, int radix)
        throws NumberFormatException {
        return valueOf(parseByte(s, radix));
    }

   // 调用吱声valueOf 默认为十的String转化
    public static Byte valueOf(String s) throws NumberFormatException {
        return valueOf(s, 10);
    }

   // 使用Integer的转换,可以自动识别s是什么进制的数字来进行转换为十进制的数字再转换为byte
    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;
    }

    // 十进制s的构建
    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;
    }

   // 输出的是Integer转换后的值
    public String toString() {
        return Integer.toString((int)value);
    }

    // byte和Intger的hashcode值就是自身
    @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;
    }

   // 用于比较,实际是两个byte的减法
    public int compareTo(Byte anotherByte) {
        return compare(this.value, anotherByte.value);
    }

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

   // 用于将byte转成无符号数据
    public static int toUnsignedInt(byte x) {
        return ((int) x) & 0xff;
    }

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


    // 表示Byte类型的位数
    public static final int SIZE = 8;

   // Byte类型占的字节数
    public static final int BYTES = SIZE / Byte.SIZE;

  
    private static final long serialVersionUID = -7183698231559129828L;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值