* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
package java.lang;
/**
*
* 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}.
*
* <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}.
*
* @author Nakul Saraiya
* @author Joseph D. Darcy
* @see java.lang.Number
* @since JDK1.1
*/
public final class Byte extends Number implements Comparable<Byte> {
// final表示Byte不可继承
// 对于byte最小值的规定
public static final byte MIN_VALUE = -128;
// 最小值
public static final byte MAX_VALUE = 127;
// 警告静默 获取byte的原始类
@SuppressWarnings("unchecked")
public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
// 将byte转为String 十位限制
public static String toString(byte b) {
return Integer.toString((int)b, 10);
}
// 通过内部类创建一个具有全部的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));
}
}
// 数组的0位置是-128 所以想从数组中获取到对应的对象必须要加128来确定位置
//
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
// 通过Integer的转换方法 将radix位的s转为int 当没有超出范围就转成byte
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);
}
// 先将转换成Byte 再放入valueOf中转化成取得数组中的对象
public static Byte valueOf(String s, int radix)
throws NumberFormatException {
return valueOf(parseByte(s, radix));
}
// 调用吱声valueOf 默认为十的String转化
public static Byte valueOf(String s) throws NumberFormatException {
return valueOf(s, 10);
}
// 使用Integer的转换,可以自动识别s是什么进制的数字来进行转换为十进制的数字再转换为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);
}
// 内容值
private final byte value;
// 构建器
public Byte(byte value) {
this.value = value;
}
// 十进制s的构建
public Byte(String s) throws NumberFormatException {
this.value = parseByte(s, 10);
}
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;
}
// 输出的是Integer转换后的值
public String toString() {
return Integer.toString((int)value);
}
// byte和Intger的hashcode值就是自身
@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;
}
// 用于比较,实际是两个byte的减法
public int compareTo(Byte anotherByte) {
return compare(this.value, anotherByte.value);
}
public static int compare(byte x, byte y) {
return x - y;
}
// 用于将byte转成无符号数据
public static int toUnsignedInt(byte x) {
return ((int) x) & 0xff;
}
public static long toUnsignedLong(byte x) {
return ((long) x) & 0xffL;
}
// 表示Byte类型的位数
public static final int SIZE = 8;
// Byte类型占的字节数
public static final int BYTES = SIZE / Byte.SIZE;
private static final long serialVersionUID = -7183698231559129828L;
}