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

JDK源码学习与分析之Byte

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

JDK源码学习与分析之Byte

        本文基于JDK1.8学习与分析Byte源码:

package java.lang;


public final class Byte extends Number implements Comparable {

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

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

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

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

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

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

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

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

    
    public boolean equals(Object obj) {
        if (obj instanceof Byte) {
            return value == ((Byte)obj).byteValue();
        }
        return false;
    }

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

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

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

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

    
    public static final int SIZE = 8;

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

    private static final long serialVersionUID = -7183698231559129828L;
}

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

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

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