栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JDK1.8源码学习--lang包(Byte)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JDK1.8源码学习--lang包(Byte)

前言

 

月是一轮明镜,晶莹剔透,代表着一张白纸(啥也不懂)

央是一片海洋,海乃百川,代表着一块海绵(吸纳万物)

泽是一柄利剑,千锤百炼,代表着千百锤炼(输入输出)

月央泽,学习的一种过程,从白纸->吸收各种知识->不断输入输出变成自己的内容

希望大家一起坚持这个过程,也同样希望大家最终都能从零到零,把知识从薄变厚,再由厚变薄!
 

一.Byte的作用
 

        

二.Byte的类图

                a).一个Comparable,这个接口对实现他的每个对象都可按照一定的规则来进行排序,详情请点击下面链接

                  ......(假装这个是链接,以后补充)

                b).一个Number,这个抽象类是规范基础类型int,byte,short等的规范,详情请点击下面链接
                      JDK1.8源码学习--lang包(Number)_w5_Silence的博客-CSDN博客

三.成员变量:  
 
    public static final byte   MIN_VALUE = -128;

    
    public static final byte   MAX_VALUE = 127;

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

 
    private final byte value;

  
    public static final int SIZE = 8;

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

    
    private static final long serialVersionUID = -7183698231559129828L;
四.私有内部类:  

        Byte内部维护了一个ByteCache作为缓存,数组长度为256,维护了从-128到127的数值

//Byte内部维护了一个256长度的Byte缓存数组
    private static class ByteCache {
        private ByteCache(){}

        static final Byte cache[] = new Byte[-(-128) + 127 + 1];

        static {
            //缓存的数值从-128~~127
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Byte((byte)(i - 128));
        }
    }
五构造方法:
   
    public Byte(byte value) {
        this.value = value;
    }

    
    public Byte(String s) throws NumberFormatException {
        this.value = parseByte(s, 10);
    }
六.成员方法: 

                a.toString(byte b)

                        将传入的byte值变成string字符串

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

                b.valueOf(byte b)

                        返回b的Byte对象

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

                c.parseByte(String s,int radix)

                        将字符串解析成以radix为基数的 byte值

 
    public static byte parseByte(String s, int radix)
        throws NumberFormatException {
        //调用Integer.parseInt来解析
        int i = Integer.parseInt(s, radix);
        if (i < MIN_VALUE || i > MAX_VALUE)
            throw new NumberFormatException(
                "Value out of range. Value:"" + s + "" Radix:" + radix);
        //强转成byte
        return (byte)i;
    }

    //默认以10为基数的解析
    public static byte parseByte(String s) throws NumberFormatException {
        return parseByte(s, 10);
    }

                d.valueOf()

                        将字符串解析成Byte

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

                f.decode(String nm)

                        将可解析的字符串解码成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);
    }

                g.将Byte转化为不同的其他值

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

                h.equals(Object obj)

                        判断是否与传入的值的byte值相等

    / * 判断传入的值是否与当前的byte值相等
     * 相等为true,不等为false
     */
    public boolean equals(Object obj) {
        if (obj instanceof Byte) {
            return value == ((Byte)obj).bytevalue();
        }
        return false;
    }

                i.compare

                        判断两个byte值是否相等

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

    //判断两个byte值是否相等
    public static int compare(byte x, byte y) {
        return x - y;
    }

                j.无符号转化为int或者long

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

    
    public static long toUnsignedLong(byte x) {
        return ((long) x) & 0xffL;
    }
七.总结

        对于Byte,首先实现了Number,实现了其全部抽象方法,去转化为其他值提供了函数方法,其次,该类自己也提供了一些操作byte更方便的方法,这种装饰模式就很利于我们去包装一些需要处理的类,从而形成自己的类库.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/602886.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号