Byte是基本数据类型byte的包装类。

  1)声明部分:

public final class Byte extends Number implements Comparable<Byte>

  实现Comparable<T>接口,实现该接口方法如下:

public int compareTo(Byte anotherByte) {
return compare(this.value, anotherByte.value);
}
public static int compare(byte x, byte y) {
return x - y;
}

  继承Number.java,方法如下:

public abstract int intValue();
public abstract float floatValue();
public abstract long longValue();
public abstract double doubleValue();
public byte byteValue() {
return (byte)intValue();
}
public short shortValue() {
return (short)intValue();
}

  其中前4个是抽象方法,Byte.java不是抽象类,所以必须实现父类的4个抽象方法;后2个方法实现调用第一个方法。Number提供了包装类型之间的强类型转换(JDK目前已经实现了自动拆箱和装箱操作)。

  2)属性部分

//min value
public static final byte MIN_VALUE = -128;
//max value
public static final byte MAX_VALUE = 127;
//The {@code Class} instance representing the primitive type
public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
//The value of the {@code Byte}.
private final byte value;
//The number of bits used to represent a {@code byte} value in two's complement binary form.
public static final int SIZE = 8;
//The number of bytes used to represent a {@code byte} value in two's complement binary form.
public static final int BYTES = SIZE / Byte.SIZE;

  3)私有内部静态类

//Byte Cache -128~127
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));
}
}

  含有静态模块,class加载的时候,执行静态模块,初始化cache[]。

  4)Byte的声明

  构造方法重载:

//构造方法重载1
public Byte(byte value) {
this.value = value;
}
//构造方法重载2
public Byte(String s) throws NumberFormatException {
this.value = parseByte(s, 10);
}

  类型转换为Byte的方法,除了构造方法和自动装箱外: 

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 valueOf(byte b)和public static byte parseByte(String s, int radix)是核心。第2个方法转换为byte;第一个方法转换为Byte,Byte根据byte的值,从缓存中获取Byte对象。

  例子:

//构造方法重载
byte byte1 = 1;
Byte b1 = new Byte(byte1);
Byte b2 = new Byte("1");
Byte b3 = byte1;//自动装箱
out(b1==b2);//fasle
out(b1==b3);//fasle
//初始化方法,除构造方法和自动装箱
Byte b4 = Byte.parseByte("1",10);//最终byte自动装箱
Byte b5 = Byte.parseByte("1");//同上
Byte b6 = Byte.valueOf("1",10);//最终取ByteCache
Byte b7 = Byte.valueOf("1");//同上
Byte b8 = Byte.valueOf(byte1);//同上
out(b4==b2);//fasle
out(b4==b1);//fasle
out(b4==b3);//true
out(b4==b5);//true
out(b6==b7);//true
out(b6==b8);//true

  从例子可以得出结论:采用自动装箱会默认根据byte值去获取ByteCache,而使用构造方法重载不会去获取ByteCache,而是生成一个新的对象,所以byte声明的时候,要采用直接装箱,或者除了new一个对象以外的方法。
  5)其他方法:

public static String toString(byte b) {
return Integer.toString((int)b, 10);
}
public String toString() {
return Integer.toString((int)value);
} //decode 解码,将(2,8,16)进制转换为十进制
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);
} @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 static int toUnsignedInt(byte x) {
return ((int) x) & 0xff;
}
public static long toUnsignedLong(byte x) {
return ((long) x) & 0xffL;
}

  例子:

//其他方法
Byte.toString(byte1);//1
b8.toString();//1
b8.hashCode();//1
Byte.decode("0x8");//8
b4.equals(b2);//true
byte byte2 = -1;
Byte.toUnsignedInt(byte1);//1
Byte.toUnsignedLong(byte1);//1
Byte.toUnsignedInt(byte2);//255 强转为int的-1,-1的二进制补码:1111,1111,1111,1111,1111,1111,1111,1111,进行& 0xff,截取byte表示8位以外的其他高位。
Byte.toUnsignedLong(byte2);//255

  

JDK源码分析:Byte.java的更多相关文章

  1. 【jdk源码分析】java.lang.Appendable

    1.概述 public interface Appendable 能够被添加 char 序列和值的对象.如果某个类的实例打算接收取自 Formatter 的格式化输出,那么该类必须实现 Appenda ...

  2. 【jdk源码分析】java多线程开启的三种方式

    1.继承Thread类,新建一个当前类对象,并且运行其start()方法 package com.xiaostudy.thread; /** * @desc 第一种开启线程的方式 * @author ...

  3. JDK源码之Byte类分析

    一 简介 byte,即字节,由8位的二进制组成.在Java中,byte类型的数据是8位带符号的二进制数,以二进制补码表示的整数 取值范围:默认值为0,最小值为-128(-2^7);最大值是127(2^ ...

  4. JDK源码分析—— ArrayBlockingQueue 和 LinkedBlockingQueue

    JDK源码分析—— ArrayBlockingQueue 和 LinkedBlockingQueue 目的:本文通过分析JDK源码来对比ArrayBlockingQueue 和LinkedBlocki ...

  5. JDK 源码分析(4)—— HashMap/LinkedHashMap/Hashtable

    JDK 源码分析(4)-- HashMap/LinkedHashMap/Hashtable HashMap HashMap采用的是哈希算法+链表冲突解决,table的大小永远为2次幂,因为在初始化的时 ...

  6. JDK源码分析(5)Vector

    JDK版本 Vector简介 /** * The {@code Vector} class implements a growable array of * objects. Like an arra ...

  7. JDK源码分析(2)LinkedList

    JDK版本 LinkedList简介 LinkedList 是一个继承于AbstractSequentialList的双向链表.它也可以被当作堆栈.队列或双端队列进行操作. LinkedList 实现 ...

  8. 【JDK】JDK源码分析-HashMap(1)

    概述 HashMap 是 Java 开发中最常用的容器类之一,也是面试的常客.它其实就是前文「数据结构与算法笔记(二)」中「散列表」的实现,处理散列冲突用的是“链表法”,并且在 JDK 1.8 做了优 ...

  9. 【JDK】JDK源码分析-ArrayList

    概述 ArrayList 是 List 接口的一个实现类,也是 Java 中最常用的容器实现类之一,可以把它理解为「可变数组」. 我们知道,Java 中的数组初始化时需要指定长度,而且指定后不能改变. ...

  10. 【JDK】JDK源码分析-AbstractQueuedSynchronizer(1)

    概述 前文「JDK源码分析-Lock&Condition」简要分析了 Lock 接口,它在 JDK 中的实现类主要是 ReentrantLock (可译为“重入锁”).ReentrantLoc ...

随机推荐

  1. Gradle Goodness: Working with Live Task Collection

    Gradle support the definition of so called live collections. These collections are mostly created ba ...

  2. win7下添加库文件出现“file is not regcognized”问题

    最近几天需要画电路图,所以安装了protel se99,安装后在添加库文件的时候出现“file is not regcognized”的问题 百度查了一下,说win7基本上都会出现这个问题. 实际上, ...

  3. 去掉CodeIgniter URL中的index.php

    CI默认的rewrite url中是类似这样的,例如你的CI根目录是在/CodeIgniter/下,你的下面的二级url就类似这样http://localhost /CodeIgniter/index ...

  4. linux mysql命令行查看显示中文

    linux 命令行查看mysql的库字符集是utf8,查询某个表时,仍然是显示不了中文, 之后使用了命令 mysql>set  names utf8;就可以正常显示中文了. 如何才更好的使mys ...

  5. Python开发工具之Sublime Text 3基于文件创建项目

    说明: 本地windows系统 本地已安装Sublime Text 3; 本地已创建python项目文件,如test,并在该文件夹下创建了虚拟环境venv(test/venv). 1.创建项目 依次鼠 ...

  6. 用javascript编写简单银行取钱存钱流程(函数)

    const readline = require('readline-sync')//引用readline-sync let arr = [[], []]; //登陆 let add = functi ...

  7. spring boot从redis取缓存发生java.lang.ClassCastException异常

    目录树 异常日志信息 错误原因 解决方法 异常日志信息 2018-09-24 15:26:03.406 ERROR 13704 --- [nio-8888-exec-8] o.a.c.c.C.[.[. ...

  8. string::size_type类型

    string::size_type类型 对于string中的size函数,size函数返回的是string对象的字符个数(长度),我们知道,对size()来说,返回一个int或者是一个unsigned ...

  9. .Net 上传文件到ftp服务器和下载文件

    突然发现又很久没有写博客了,想起哎呦,还是写一篇博客记录一下吧,虽然自己还是那个渣渣猿. 最近在做上传文件的功能,上传到ftp文件服务器有利于管理上传文件. 前面的博客有写到layui如何上传文件,然 ...

  10. 线上服务内存OOM问题定位三板斧

    相信大家都有感触,线上服务内存OOM的问题,是最难定位的问题,不过归根结底,最常见的原因: 本身资源不够 申请的太多 资源耗尽 58到家架构部,运维部,58速运技术部联合进行了一次线上服务内存OOM问 ...