Byte
byte是java基本数据类型之一,是java中长度最小的基本数字类型,并且我们在读写文件时经常使用byte数组,Byte是其包装类,现在我们一起去看看它的实现吧!
类图
1
2public final class Byte extends Number implements Comparable<Byte>
通过类图和源码我们可以知道Byte是不可被继承的,并且Byte的对象是可以比较的,由于Byte继承了Number(这是一个抽象类),所以Byte重写了其所有形如xxxValue
的方法。
成员变量
1
2
3
4
5
6
7public static final byte MIN_VALUE = -128; public static final byte MAX_VALUE = 127; public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");//获取Byte的class private final byte value; public static final int SIZE = 8;//表示二进制补码形式的byte值的位数 public static final int BYTES = SIZE / Byte.SIZE;//表示二进制补码形式的byte值的字节数
Byte对象的值保持在value中,并且value是不可变的。
构造方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public Byte(byte value) { this.value = value; } 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; }
Byte有两个构造方法,一个是直接接受一个byte类型的值即可,将其赋值给value;另一个接受一个字符串类型数据(必须是数字字符串),通过parseByte
方法将其转换,如果字符串为null或长度为0(如果首字符为-或+长度要大于1)则抛出NumberFormatException
异常,传递的字符串值区间为[-128,127],超出此范围抛出NumberFormatException("Value out of range. Value:"" + s + "" Radix:" + radix)
异常信息。
valueOf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15private 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]; }
Byte实现了ByteCache这一静态内部类,不看代码也能猜测出此类由于缓存Byte数据,果不其然,cache[]
存储了-128~127
之间的所有数值。在使用valueOf
获取Byte实例对象时都是获取cache[]
中的对象(多次获取的都是同一个对象呀~),如获取-128,即cache[-128+128]=cache[0]
,使用offset这个技巧很巧妙呀!
1
2
3
4
5
6
7
8public 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); }
java同时实现了通过string获取Byte对象的valueOf
方法,我们仅仅需要注意的是字符串必须是合法的字符串,并且值在[-128,127]这个区间,其他的和上文的valueOf一致。
decode
1
2
3
4
5
6
7
8public 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); }
decode
方法将字符串解码为Byte,此方法接受十进制字符串,十六进制字符串(用0xX#标识
),八进制字符串(用0标识
),但是解码后的值必须在[-128,127]这一区间内,否则抛出NumberFormatException
异常。
xxxValue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19public byte byteValue() { return value; } public short shortValue() { return (short)value; } public float floatValue() { return (float)value; } public double doubleValue() { return (double)value; } public int intValue() { return (int)value; } public long longValue() { return (long)value; }
这六个方法都是继承自Number
类,将value值强转为对应的类型。
hashCode
1
2
3
4
5
6
7public int hashCode() { return Byte.hashCode(value); } public static int hashCode(byte value) { return (int)value; }
Byte对象的hash值即为其value值。
equals
1
2
3
4
5
6
7public boolean equals(Object obj) { if (obj instanceof Byte) { return value == ((Byte)obj).byteValue(); } return false; }
首先判断obj是否为Byte的实例,再判断value是否相同。
compareTo
1
2
3
4
5
6
7public int compareTo(Byte anotherByte) { return compare(this.value, anotherByte.value); } public static int compare(byte x, byte y) { return x - y; }
返回大于0前者大于后者,等于0两者相等,小于0前者小于后者。
toUnsignedInt
jdk1.8后新增方法,将byte数据转换为无符号的int数据,也就是byte类型数据与0xff
进行&
操作后的值。与此方法类似的还有toUnsignedLong
。
文章同步【个人站】
最后
以上就是壮观高山最近收集整理的关于JDK源码阅读之Byte的全部内容,更多相关JDK源码阅读之Byte内容请搜索靠谱客的其他文章。
发表评论 取消回复