月是一轮明镜,晶莹剔透,代表着一张白纸(啥也不懂)
央是一片海洋,海乃百川,代表着一块海绵(吸纳万物)
泽是一柄利剑,千锤百炼,代表着千百锤炼(输入输出)
月央泽,学习的一种过程,从白纸->吸收各种知识->不断输入输出变成自己的内容
希望大家一起坚持这个过程,也同样希望大家最终都能从零到零,把知识从薄变厚,再由厚变薄!
二.Byte的类图
a).一个Comparable,这个接口对实现他的每个对象都可按照一定的规则来进行排序,详情请点击下面链接
......(假装这个是链接,以后补充)
b).一个Number,这个抽象类是规范基础类型int,byte,short等的规范,详情请点击下面链接
JDK1.8源码学习--lang包(Number)_w5_Silence的博客-CSDN博客
public static final byte MIN_VALUE = -128;
public static final byte MAX_VALUE = 127;
@SuppressWarnings("unchecked")
public static final Class TYPE = (Class) Class.getPrimitiveClass("byte");
private final byte value;
public static final int SIZE = 8;
public static final int BYTES = SIZE / Byte.SIZE;
private static final long serialVersionUID = -7183698231559129828L;
四.私有内部类:
Byte内部维护了一个ByteCache作为缓存,数组长度为256,维护了从-128到127的数值
//Byte内部维护了一个256长度的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));
}
}
五构造方法:
public Byte(byte value) {
this.value = value;
}
public Byte(String s) throws NumberFormatException {
this.value = parseByte(s, 10);
}
六.成员方法:
a.toString(byte b)
将传入的byte值变成string字符串
public static String toString(byte b) {
return Integer.toString((int)b, 10);
}
b.valueOf(byte b)
返回b的Byte对象
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
c.parseByte(String s,int radix)
将字符串解析成以radix为基数的 byte值
public static byte parseByte(String s, int radix)
throws NumberFormatException {
//调用Integer.parseInt来解析
int i = Integer.parseInt(s, radix);
if (i < MIN_VALUE || i > MAX_VALUE)
throw new NumberFormatException(
"Value out of range. Value:"" + s + "" Radix:" + radix);
//强转成byte
return (byte)i;
}
//默认以10为基数的解析
public static byte parseByte(String s) throws NumberFormatException {
return parseByte(s, 10);
}
d.valueOf()
将字符串解析成Byte
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);
}
f.decode(String nm)
将可解析的字符串解码成Byte
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);
}
g.将Byte转化为不同的其他值
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;
}
public String toString() {
return Integer.toString((int)value);
}
@Override
public int hashCode() {
return Byte.hashCode(value);
}
public static int hashCode(byte value) {
return (int)value;
}
h.equals(Object obj)
判断是否与传入的值的byte值相等
/ * 判断传入的值是否与当前的byte值相等
* 相等为true,不等为false
*/
public boolean equals(Object obj) {
if (obj instanceof Byte) {
return value == ((Byte)obj).bytevalue();
}
return false;
}
i.compare
判断两个byte值是否相等
public int compareTo(Byte anotherByte) {
return compare(this.value, anotherByte.value);
}
//判断两个byte值是否相等
public static int compare(byte x, byte y) {
return x - y;
}
j.无符号转化为int或者long
public static int toUnsignedInt(byte x) {
return ((int) x) & 0xff;
}
public static long toUnsignedLong(byte x) {
return ((long) x) & 0xffL;
}
七.总结
对于Byte,首先实现了Number,实现了其全部抽象方法,去转化为其他值提供了函数方法,其次,该类自己也提供了一些操作byte更方便的方法,这种装饰模式就很利于我们去包装一些需要处理的类,从而形成自己的类库.



