`

Byte 类源码分析

阅读更多

说明:Byte 类的实现,有一部分方法是借助于 Integer 类的实现.

/*
* Copyright (c) 1996, 2009, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/

package jdk.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> {

    /**
     * byte 最小值的常量{@code byte}可以具有-2 ^ 7.
     * Java 中用补码表示二进制数, 补码最高位表示符号位,0 表示正数,1 表示负数.
     * 正数补码为其本身; 负数补码为其各位取反加1;
     */
    public static final byte   MIN_VALUE = -128;

    /**
     * byte 最大值为 2 ^ 7 -1.
     */
    public static final byte   MAX_VALUE = 127;

    /**
     * The {@code Class} instance representing the primitive type
     * {@code byte}.
     */
    public static final Class<Byte>     TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");

    /**
     * 返回表示指定的{@code byte}的新{@code String}对象. 假设基数为10.
     *
     * @param b the {@code byte} to be converted
     * @return the string representation of the specified {@code byte}
     * @see java.lang.Integer#toString(int)
     */
    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 {
            // 缓存初始化,范围是-128到127
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Byte((byte)(i - 128));
        }
    }

    /**
     * 封装基础byte到Byte对象,不过就是从缓存数组中取出对应的值.
     * 说明:这里的这种处理方法和 Boolean 中的 valueOf 方法的处理方式类似,都是先定义
     * 好了取值范围,然后直接返回定义好的值的引用即可.
     *
     * @param  b a byte value.
     * @return a {@code Byte} instance representing {@code b}.
     * @since  1.5
     */
    public static Byte valueOf(byte b) {
        final int offset = 128;
        return ByteCache.cache[(int)b + offset];
    }

    /**
     * 将字符串按照指定的基数 radix 解析成 byte.
     *
     * @param s         the {@code String} containing the
     *                  {@code byte}
     *                  representation to be parsed
     * @param radix     the radix to be used while parsing {@code s}
     * @return          the {@code byte} value represented by the string
     *                   argument in the specified radix
     * @throws          NumberFormatException If the string does
     *                  not contain a parsable {@code 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;
    }

    /**
     * 将字符串 s 解析成 基数为 10 进制的 byte.
     *
     * @param s         a {@code String} containing the
     *                  {@code byte} representation to be parsed
     * @return          the {@code byte} value represented by the
     *                  argument in decimal
     * @throws          NumberFormatException if the string does not
     *                  contain a parsable {@code byte}.
     */
    public static byte parseByte(String s) throws NumberFormatException {
        return parseByte(s, 10);
    }

    /**
     * 将字符串按照指定的基数 radix 解析成 Byte.
     *
     * @param s         the string to be parsed
     * @param radix     the radix to be used in interpreting {@code s}
     * @return          a {@code Byte} object holding the value
     *                  represented by the string argument in the
     *                  specified radix.
     * @throws          NumberFormatException If the {@code String} does
     *                  not contain a parsable {@code byte}.
     */
    public static Byte valueOf(String s, int radix)
        throws NumberFormatException {
        return valueOf(parseByte(s, radix));
    }

    /**
     * 将字符串 s 解析成 基数为 10 进制的 Byte.
     *
     * @param s         the string to be parsed
     * @return          a {@code Byte} object holding the value
     *                  represented by the string argument
     * @throws          NumberFormatException If the {@code String} does
     *                  not contain a parsable {@code byte}.
     */
    public static Byte valueOf(String s) throws NumberFormatException {
        return valueOf(s, 10);
    }

    /**
     * 将{@code String}解码为{@code Byte}. 接受以下语法给出的十进制,十六进制和八进制数:
     * 0x|0X|#|0
     *
     * @param     nm the {@code String} to decode.
     * @return   a {@code Byte} object holding the {@code byte}
     *          value represented by {@code nm}
     * @throws  NumberFormatException  if the {@code String} does not
     *            contain a parsable {@code byte}.
     * @see java.lang.Byte#parseByte(java.lang.String, int)
     */
    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);
    }

    /**
     * The value of the {@code Byte}.
     *
     * @serial
     */
    private final byte value;

    /**
     * 构造一个新分配的{@code Byte}对象,该对象表示指定的{@code byte}值.
     *
     * @param value     the value to be represented by the
     *                  {@code Byte}.
     */
    public Byte(byte value) {
        this.value = value;
    }

    /**
     * 构造一个新分配的{@code Byte}对象,该对象表示{@code String}参数指示的{@code byte}值.
     * 字符串将以{@code parseByte}方法用于基数10的方式转换为{@code byte}值.
     *
     * @param s         the {@code String} to be converted to a
     *                  {@code Byte}
     * @throws           NumberFormatException If the {@code String}
     *                  does not contain a parsable {@code byte}.
     * @see        java.lang.Byte#parseByte(java.lang.String, int)
     */
    public Byte(String s) throws NumberFormatException {
        this.value = parseByte(s, 10);
    }

    /**
     * Returns the value of this {@code Byte} as a
     * {@code byte}.
     */
    public byte byteValue() {
        return value;
    }

    /**
     * Returns the value of this {@code Byte} as a
     * {@code short}.
     */
    public short shortValue() {
        return (short)value;
    }

    /**
     * Returns the value of this {@code Byte} as an
     * {@code int}.
     */
    public int intValue() {
        return (int)value;
    }

    /**
     * Returns the value of this {@code Byte} as a
     * {@code long}.
     */
    public long longValue() {
        return (long)value;
    }

    /**
     * Returns the value of this {@code Byte} as a
     * {@code float}.
     */
    public float floatValue() {
        return (float)value;
    }

    /**
     * Returns the value of this {@code Byte} as a
     * {@code double}.
     */
    public double doubleValue() {
        return (double)value;
    }

    /**
     * Returns a {@code String} object representing this
     * {@code Byte}'s value.  The value is converted to signed
     * decimal representation and returned as a string, exactly as if
     * the {@code byte} value were given as an argument to the
     * {@link java.lang.Byte#toString(byte)} method.
     *
     * @return  a string representation of the value of this object in
     *          base 10.
     */
    public String toString() {
        return Integer.toString((int)value);
    }

    /**
     * Returns a hash code for this {@code Byte}; equal to the result
     * of invoking {@code intValue()}.
     *
     * @return a hash code value for this {@code Byte}
     */
    public int hashCode() {
        return (int)value;
    }

    /**
     * Compares this object to the specified object.  The result is
     * {@code true} if and only if the argument is not
     * {@code null} and is a {@code Byte} object that
     * contains the same {@code byte} value as this object.
     *
     * @param obj       the object to compare with
     * @return          {@code true} if the objects are the same;
     *                  {@code false} otherwise.
     */
    public boolean equals(Object obj) {
        if (obj instanceof Byte) {
            return value == ((Byte)obj).byteValue();
        }
        return false;
    }

    /**
     * Compares two {@code Byte} objects numerically.
     *
     * @param   anotherByte   the {@code Byte} to be compared.
     * @return  the value {@code 0} if this {@code Byte} is
     *          equal to the argument {@code Byte}; a value less than
     *          {@code 0} if this {@code Byte} is numerically less
     *          than the argument {@code Byte}; and a value greater than
     *           {@code 0} if this {@code Byte} is numerically
     *           greater than the argument {@code Byte} (signed
     *           comparison).
     * @since   1.2
     */
    public int compareTo(Byte anotherByte) {
        return compare(this.value, anotherByte.value);
    }

    /**
     * 以数字方式比较两个{@code byte}值.
     *
     * <pre>
     *    Byte.valueOf(x).compareTo(Byte.valueOf(y))
     * </pre>
     *
     * @param  x the first {@code byte} to compare
     * @param  y the second {@code byte} to compare
     * @return the value {@code 0} if {@code x == y};
     *         a value less than {@code 0} if {@code x < y}; and
     *         a value greater than {@code 0} if {@code x > y}
     * @since 1.7
     */
    public static int compare(byte x, byte y) {
        return x - y;
    }

    /**
     * The number of bits used to represent a {@code byte} value in two's
     * complement binary form.
     *
     * @since 1.5
     */
    public static final int SIZE = 8;

    /** use serialVersionUID from JDK 1.1. for interoperability */
    private static final long serialVersionUID = -7183698231559129828L;
}
1
0
分享到:
评论

相关推荐

    wangwangla#qiuzhao#第三章 Lucene的源码分析1

    反向信息词---&gt;文档文件有:数据类型:Lucene索引文件中Byte:最基本的数据类型Uint32:由4个byte组成VInt:长度变化的数据类型,可能包含多

    Collections源码java-jdk1.8-source-analysis:Java8源码分析,J.U.C、ThreadPool、Col

    JDK1.8源码分析 导入源码过程中的注意事项 JDK1.8对应JDK版本下载: 提取码:49wi 源码在src目录下 以下两个类手动添加的,解决编译过程中该包的丢失 sun.font.FontConfigManager sun.awt.UNIXToolkit 其中: 1.请...

    jdk1.8-source-analysis:JDK1.8源码分析

    jdk1.8-source-analysis JDK1.8源码分析引入原始过程中的注意事项JDK1.8对应JDK版本下载: 码:49wi原始码在src目录下以下两个类手动添加的,解决编译过程中该包的丢失sun.font.FontConfigManager sun.awt....

    jdk1.8-source-analysis:Java 8源码分析,JUC,ThreadPool,Collection

    JDK1.8源码分析 引入原始过程中的注意事项 JDK1.8对应JDK版本下载: 码:49wi 原始码在src目录下 以下两个类手动添加的,解决编译过程中该包的丢失 sun.font.FontConfigManager sun.awt.UNIXToolkit 其中:1.请...

    java源码剖析-JavaSourceLearn:JDK1.8源码的代码分析和学习

    JDK1.8源码分析 导入源码过程中的注意事项 源码在%JAVA_HOME%\src.zip 源码在src目录下 以下两个类手动添加的,解决编译过程中该包的丢失 sun.font.FontConfigManager sun.awt.UNIXToolkit 其中: 1.请手动添加jdk...

    pyquery报错:UnicodeDecodeError: ‘gbk’ codec can’t decode byte

    今天想使用pyquery库读取本地HTML文件时报错:UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xa1 in position 164: illegal multibyte sequence。...  排错过程: 我通过查看pyquery源码,企

    jdk8-source-code:jdk源码解析

    jdk1.8-source-analysis JDK1.8源码分析引入原始过程中的注意事项JDK1.8对应JDK版本下载: 码:49wi原始码在src目录下以下两个类手动添加的,解决编译过程中该包的丢失sun.font.FontConfigManager sun.awt....

    JVM调优工具Jprofiler内存泄漏定位的简单分析(附源码)(二)

    JVM调优工具Jprofiler内存泄漏定位的简单分析(附源码)(二)

    精易模块[源码] V5.15

    3、新增“类回调_取类地址”类回调(带实例)源码来自东灿的ECallBack。 4、新增“类回调_释放回调”释放回调子程序地址。 5、新增“系统_调用帮助”调用系统帮助文档。 精易模块 V3.81 what’s new:(20140811) 1...

    VB 取 MAC的两种方法源码与工具

    最近有幸分析了一下取MAC地址的大量代码,提炼总结了一下,编了个小工具(为封装测试过程的衍生品),可用。并附有关键源码(试着点击对话框,会显示)。 VB API 调用可以解决取 MAC 地址。需要知道MAC数据块的偏移...

    基于VHDL的I2C设计源码

    自己进行了新的思路设计,利用状态机实现。因为第一次自己动手写,所以写得相对幼稚(现在看来),但是实现了i2c主功能,...在XILINX的FPGA上实现,且用周立功逻辑分析仪中I2C插件进行了验证。包括BYTE写、页读写等。

    arc4random:arc4random()函数子集的本机绑定

    arc4random arc4random()家族的本地绑定: arc4random() arc4random_buf(buf, nbytes) arc4random_uniform(upper_...// stores nbytes integers in the given buffer.// each byte is an integer in the range [0, 256)

    java7hashmap源码-aa:面试资料

    hashmap源码 - - - - - - - - - - 基础篇 1、 Java语言有哪些特点 1、简单易学、有丰富的类库 2、面向对象(Java最重要的特性,让程序耦合度更低,内聚性更高) 3、与平台无关性(JVM是Java跨平台使用的根本) 4、...

    java7hashmap源码-JavaInterview:Java面试汇总

    hashmap源码 『Java面试』 - - - - - - - - - - - - - - - - - 基础篇 1、 Java语言有哪些特点 1、简单易学、有丰富的类库 2、面向对象(Java最重要的特性,让程序耦合度更低,内聚性更高) 3、与平台无关性(JVM是...

    java7hashmap源码-JavaInterview:适用于Java面试的文档

    hashmap源码 JavaInterview - - - - - - - - - - - - - - - - - 基础篇 1、 Java语言有哪些特点 1、简单易学、有丰富的类库 2、面向对象(Java最重要的特性,让程序耦合度更低,内聚性更高) 3、与平台无关性(JVM是...

    python源码剖析(样章)

    在最高的层次上,Python 的整体架构可以...—Python 字节码(byte code),就像 Java 编译器和 C#编译器所做的那样;最 后由 Code Evaluator 来解释并执行这些字节码。因此,Code Evaluator 又可以 被称为执行引擎。

    基于Python和条形图实现的世界杯数据可视化源码+详细项目说明+数据.tar

    基于Python和条形图实现的世界杯数据可视化源码+详细项目说明+数据.tar 1 前言 要说时下最热门的活动,非2022卡塔尔世界杯莫属了。正好借此次机会,回顾一下过往的世界杯,就其历史数据做一些数据的呈现和分析工作...

    大智慧股票本地数据读取接口(含源码)

    18 - 21 31 41 30 30 30...FF 证券代码 byte[10] 22 - 25 B0 09 00 00 日线记录数 int 26 - 57 00 00 25 04...FF FF 记录块号 char[25] ...... 记录块开始于0x41000 41000 - 41003 80 47 B2 2B 日期 int 41004 - ...

    java 面试题 总结

    新类继承了原始类的特性,新类称为原始类的派生类(子类),而原始类称为新类的基类(父类)。派生类可以从它的基类那里继承方法和实例变量,并且类可以修改或增加新的方法使之更适合特殊的需要。 3.封装: 封装是把...

    net学习笔记及其他代码应用

    声明方法的存在而不去实现它的类被叫做抽象类(abstract class),它用于要创建一个体现某些基本行为的类,并为该类声明方法,但不能在该类中实现该类的情况。不能创建abstract 类的实例。然而可以创建一个变量,其...

Global site tag (gtag.js) - Google Analytics