文章目录
1.概述
byte是java基本数据类型之一,是java中长度最小的基本数字类型,并且我们在读写文件时经常使用byte数组,Byte是其包装类,由final修饰不可被继承。很多方法的实现借助了Integer类。
2.类图
继承了Number类,可以用于数字类型的一系列转换
实现了Comparable接口,可以用于比较
实现了序列化接口,用于序列化和反序列
3.属性
public static final byte MIN_VALUE = -128; 静态常量,byte的最小值
public static final byte MAX_VALUE = 127;静态常量,byte的最大值
即byte表示 -128~127的整数值
public static final Class TYPE = (Class) Class.getPrimitiveClass(“byte”);静态常量 Byte的class对象。
private final byte value; final修饰的用于存储byte值的字段。
public static final int SIZE = 8; 静态常量 表明了一个Byte的大小 8位即一个字节
public static final int BYTES = SIZE / Byte.SIZE; 静态常量 表明了一个Byte 占一个字节。
private static final long serialVersionUID = -7183698231559129828L;静态常量 序列化版本id
4.构造方法
-
public Byte(byte value)
将byte封装成Byte对象。
-
public Byte(String s)
内部调用了Integer类中的parseInt(String s, int radix)方法,将字符串s解析成int类型的数值。若i在byte的范围内则返回(byte)i 否则抛出异常
public Byte(String s) throws NumberFormatException {
this.value = parseByte(s, 10);
}
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;
}
5.ByteCache
Byte缓存,创建了一个256大小的Byte对象数组,将-128~127的值的对象都创建好放入大Byte对象数组中
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));
}
}
6.valueOf
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
将byte类型封装成Byte 直接返回的是缓存中的值。偏移128是因为数组是从下标0开始的,而存储在第0位的值是-128.
public static Byte valueOf(String s, int radix)
throws NumberFormatException {
return valueOf(parseByte(s, radix));
}
调用parseByte现将字符串解析成byte,在调用第一个方法来得到Byte对象。radix表示进制数
public static Byte valueOf(String s) throws NumberFormatException {
return valueOf(s, 10);
}
内部调用第二个方法,radix默认位十进制数
7.decode
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);
}
将字符串编码为Byte对象,内部调用Integer的decode方法。之后解析Integer的时候再看。先将字符串解析成int,然后强转为byte,在调用valueOf方法,得到Byte对象。
8.XXValue()
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;
}
然后强制转换后的结果。
9.toString
public String toString() {
return Integer.toString((int)value);
}
内部调用Integer的toString
10.hashCode
public int hashCode() {
return Byte.hashCode(value);
}
public static int hashCode(byte value) {
return (int)value;
}
Byte的hash值和value值一样。
11.equals/compareTo(Byte anotherByte)
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;
}
调用者的value比anoterBytede的value比较,返回值小于-1 则说明调用者小,返回值等于0 说明一样大,返回值大于0,则说明调用者大