JDK源码分析:Byte.java
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的更多相关文章
- 【jdk源码分析】java.lang.Appendable
1.概述 public interface Appendable 能够被添加 char 序列和值的对象.如果某个类的实例打算接收取自 Formatter 的格式化输出,那么该类必须实现 Appenda ...
- 【jdk源码分析】java多线程开启的三种方式
1.继承Thread类,新建一个当前类对象,并且运行其start()方法 package com.xiaostudy.thread; /** * @desc 第一种开启线程的方式 * @author ...
- JDK源码之Byte类分析
一 简介 byte,即字节,由8位的二进制组成.在Java中,byte类型的数据是8位带符号的二进制数,以二进制补码表示的整数 取值范围:默认值为0,最小值为-128(-2^7);最大值是127(2^ ...
- JDK源码分析—— ArrayBlockingQueue 和 LinkedBlockingQueue
JDK源码分析—— ArrayBlockingQueue 和 LinkedBlockingQueue 目的:本文通过分析JDK源码来对比ArrayBlockingQueue 和LinkedBlocki ...
- JDK 源码分析(4)—— HashMap/LinkedHashMap/Hashtable
JDK 源码分析(4)-- HashMap/LinkedHashMap/Hashtable HashMap HashMap采用的是哈希算法+链表冲突解决,table的大小永远为2次幂,因为在初始化的时 ...
- JDK源码分析(5)Vector
JDK版本 Vector简介 /** * The {@code Vector} class implements a growable array of * objects. Like an arra ...
- JDK源码分析(2)LinkedList
JDK版本 LinkedList简介 LinkedList 是一个继承于AbstractSequentialList的双向链表.它也可以被当作堆栈.队列或双端队列进行操作. LinkedList 实现 ...
- 【JDK】JDK源码分析-HashMap(1)
概述 HashMap 是 Java 开发中最常用的容器类之一,也是面试的常客.它其实就是前文「数据结构与算法笔记(二)」中「散列表」的实现,处理散列冲突用的是“链表法”,并且在 JDK 1.8 做了优 ...
- 【JDK】JDK源码分析-ArrayList
概述 ArrayList 是 List 接口的一个实现类,也是 Java 中最常用的容器实现类之一,可以把它理解为「可变数组」. 我们知道,Java 中的数组初始化时需要指定长度,而且指定后不能改变. ...
- 【JDK】JDK源码分析-AbstractQueuedSynchronizer(1)
概述 前文「JDK源码分析-Lock&Condition」简要分析了 Lock 接口,它在 JDK 中的实现类主要是 ReentrantLock (可译为“重入锁”).ReentrantLoc ...
随机推荐
- 身份认证系统(三)什么是OAuth2
本文准备用最简单的语言告诉大家什么是OAuth2 ,OAuth2是干什么的. 我们有一个资源服务器,资源服务器中有一系列的用户数据. 现在有一个应用想想要获取我们的用户数据. 那么最简单的方法就是我们 ...
- DB数据源之SpringBoot+MyBatis踏坑过程(七)手动使用Tomcat连接池
DB数据源之SpringBoot+MyBatis踏坑过程(七)手动使用Tomcat连接池 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybatis踏坑 ...
- 最优贸易(tarjan,spfa)
题目描述 C国有n个大城市和m 条道路,每条道路连接这 n个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双向通行的道 ...
- 811. Subdomain Visit Count (5月23日)
解答 class Solution { public: vector<string> subdomainVisits(vector<string>& cpdomains ...
- django的验证码
pip install Pillow==3.4.1在views.py中创建一个视图函数 from PIL import Image, ImageDraw, ImageFont from django. ...
- 指针小白:修改*p与p会对相应的地址的变量产生什么影响?各个变量指针的长度为多少?
这两天敲代码碰到了一个这样的问题 代码如下: #include <stdio.h> #include <stdlib.h> int main() { ; int* p=& ...
- vue cli3超详细创建多页面配置
1.首先按照vue cli3 给的入门文档下载个vue cli3 如果之前下载了vue cli2的要先卸载之前的 2.检查安装是否成功 3.ok,现在环境搭建好了,新建项目 vue create he ...
- CSS基础part1
CSS 概述CSS 指层叠样式表 (Cascading Style Sheets),样式定义了如何显示 HTML文件中的标签元素,CSS是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标 ...
- PHP实现识别带emoji表情的字符串
function have_special_char($str) { $length = mb_strlen($str); $array = []; for ($i=0; $i<$length; ...
- object转List<XXX>的问题
List<object> demo(object a) { List<object> res = new List<object>(); object c = a; ...