前言
月是一轮明镜,晶莹剔透,代表着一张白纸(啥也不懂)
央是一片海洋,海乃百川,代表着一块海绵(吸纳万物)
泽是一柄利剑,千锤百炼,代表着千百锤炼(输入输出)
月央泽,学习的一种过程,从白纸->吸收各种知识->不断输入输出变成自己的内容
希望大家一起坚持这个过程,也同样希望大家最终都能从零到零,把知识从薄变厚,再由厚变薄!
一.Byte的作用
/**
*
* The {@code Byte} class wraps a value of primitive type {@code byte}
* in an object. An object of type {@code Byte} contains a single
* field whose type is {@code byte}.
* 1.Byte是byte的包装类,里面包含了一个类型为byte的字段
* <p>In addition, this class provides several methods for converting
* a {@code byte} to a {@code String} and a {@code String} to a {@code
* byte}, as well as other constants and methods useful when dealing
* with a {@code byte}.
* 2.该类提供了将byte转化为String和String转化为byte的方法,也包含了一些处理byte的有用的方法
*
* @author Nakul Saraiya
* @author Joseph D. Darcy
* @see java.lang.Number
* @since JDK1.1
*/
二.Byte的类图
a).一个Comparable,这个接口对实现他的每个对象都可按照一定的规则来进行排序,详情请点击下面链接
......(假装这个是链接,以后补充)
b).一个Number,这个抽象类是规范基础类型int,byte,short等的规范,详情请点击下面链接
JDK1.8源码学习--lang包(Number)_w5_Silence的博客-CSDN博客
三.成员变量:
/**
* 持有-128的最小常量
* A constant holding the minimum value a {@code byte} can
* have, -2<sup>7</sup>.
*/
public static final byte MIN_VALUE = -128;
/**
* 持有127的最大常量
* A constant holding the maximum value a {@code byte} can
* have, 2<sup>7</sup>-1.
*/
public static final byte MAX_VALUE = 127;
/**
* The {@code Class} instance representing the primitive type
* {@code byte}.
* 表示byte的原始类型的Class对象
*/
@SuppressWarnings("unchecked")
public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
/**
* The value of the {@code Byte}.
* byte的值
* @serial
*/
private final byte value;
/**
* The number of bits used to represent a {@code byte} value in two's
* complement binary form.
* 用于以二进制补码形式表示byte值的位数
*
* @since 1.5
*/
public static final int SIZE = 8;
/**
* The number of bytes used to represent a {@code byte} value in two's
* complement binary form.
* 用于以二进制补码形式表示byte值的字节数
* @since 1.8
*/
public static final int BYTES = SIZE / Byte.SIZE;
/** use serialVersionUID from JDK 1.1. for interoperability */
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));
}
}
五构造方法:
/**
* 构造一个值为value的新的Byte对象
* Constructs a newly allocated {@code Byte} object that
* represents the specified {@code byte} value.
*
* @param value the value to be represented by the
* {@code Byte}.
*/
public Byte(byte value) {
this.value = value;
}
/**
* 1.构造一个值为s(s将字符串解析为数值)的新的Byte对象
* 2.若字符串s不能解析成byte时,将会抛出NumberFormatException
* Constructs a newly allocated {@code Byte} object that
* represents the {@code byte} value indicated by the
* {@code String} parameter. The string is converted to a
* {@code byte} value in exactly the manner used by the
* {@code parseByte} method for radix 10.
*
* @param s the {@code String} to be converted to a
* {@code Byte}
* @throws NumberFormatException If the {@code String}
* does not contain a parsable {@code byte}.
* @see java.lang.Byte#parseByte(java.lang.String, int)
*/
public Byte(String s) throws NumberFormatException {
this.value = parseByte(s, 10);
}
六.成员方法:
a.toString(byte b)
将传入的byte值变成string字符串
/**
* 将传入的byte值转化为一个String对象
* 内部调用的是Integer.toString((int)b, 10);
* 为什么是10 ? 10是jdk内部判断的一个标记,可以走更快的一个逻辑
* Returns a new {@code String} object representing the
* specified {@code byte}. The radix is assumed to be 10.
*
* @param b the {@code byte} to be converted
* @return the string representation of the specified {@code byte}
* @see java.lang.Integer#toString(int)
*/
public static String toString(byte b) {
return Integer.toString((int)b, 10);
}
b.valueOf(byte b)
返回b的Byte对象
/**
* Returns a {@code Byte} instance representing the specified
* {@code byte} value.
* 1.返回当前参数所封装的Byte值
* If a new {@code Byte} instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Byte(byte)}, as this method is likely to yield
* significantly better space and time performance since
* all byte values are cached.
* 2.如果不是必须要一个新的实例,一般优先使用当前方法
* 3.因为这方法可能会产生明显更好的空间和时间性能,因为所有的字节值都被缓存
*
* @param b a byte value.
* @return a {@code Byte} instance representing {@code b}.
* @since 1.5
*/
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
c.parseByte(String s,int radix)
将字符串解析成以radix为基数的 byte值
/**
* 1.首先这是一个带符号的解析
* 2.传入的第一个参数是待解析的字符串,当字符串不符合解析情况的时候会抛出NumberFormatException
* 3.传入的第二个参数是基数,也就是几进制,比如传入的是{ byte b = Byte.parseByte("011", 2);
* 前面是进制数,后面是进制基数,当不符合时就会抛出异常},同时基数的范围是2-36,包含2和36
* 3.字符串中第一个可为符号,而且符号仅为"+"or "-"
* 可能会抛出异常的操作:
* 1.字符串为null或者长度为零
* 2.第二个参数不在2-36
* 3.字符串仅第一个字符为"+"or"-",其他全为数字或都是数字
*/
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
/**
*
* 1.将当前可解析的字符串转化为Byte类型
* 2.第一个参数是符合以radix进制的一串数字,第一个字符可为"+"or"-"
* 3.第二个参数是告诉这个字符串代表的是什么进制
*/
public static Byte valueOf(String s, int radix)
throws NumberFormatException {
return valueOf(parseByte(s, radix));
}
/**
* 默认以10为进制来解析字符串
*/
public static Byte valueOf(String s) throws NumberFormatException {
return valueOf(s, 10);
}
f.decode(String nm)
将可解析的字符串解码成Byte
/**
* 1.将字符串解码为Byte
* 2.接收八进制,十进制,十六进制的语法
* 3.支持首位字符为"+"or "-"
* 字符签名可以是0x,0X,#或者0,将会按照语法规则来进行解析
* 字符可以是正或者负,但是中间不能有空格
*/
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转化为不同的其他值
/**
* 返回Byte所对应的byte
*/
public byte byteValue() {
return value;
}
/**
* 返回Byte所对应的short
*/
public short shortValue() {
return (short)value;
}
/**
* 返回Byte所对应的int
* @jls 5.1.2 Widening Primitive Conversions
*/
public int intValue() {
return (int)value;
}
/**
* 返回Byte所对应的long
* @jls 5.1.2 Widening Primitive Conversions
*/
public long longValue() {
return (long)value;
}
/**
* 返回Byte所对应的float
*/
public float floatValue() {
return (float)value;
}
/**
* 返回Byte所对应的double
*/
public double doubleValue() {
return (double)value;
}
/**
* 返回Byte所对应的String
*/
public String toString() {
return Integer.toString((int)value);
}
/**
* 返回Byte所对应的哈希值
*/
@Override
public int hashCode() {
return Byte.hashCode(value);
}
/**
* 返回Byte所对应的哈希值
*/
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
/**
* 1.将传入的byte值进行无符号的转化成int,其中高24为零,低八位是byte的值
* 2.零和正值会不变,负值会进行无符号的转换,比如-2就会变成254
*/
public static int toUnsignedInt(byte x) {
return ((int) x) & 0xff;
}
/**
和toUnsignenInt蕾西
*/
public static long toUnsignedLong(byte x) {
return ((long) x) & 0xffL;
}
七.总结
对于Byte,首先实现了Number,实现了其全部抽象方法,去转化为其他值提供了函数方法,其次,该类自己也提供了一些操作byte更方便的方法,这种装饰模式就很利于我们去包装一些需要处理的类,从而形成自己的类库.