您的位置:首页 > 编程语言 > Java开发

从JDK源码角度看Byte

2017-07-06 15:39 211 查看
Java的Byte类主要的作用就是对基本类型byte进行封装,提供了一些处理byte类型的方法,比如byte到String类型的转换方法或String类型到byte类型的转换方法,当然也包含与其他类型之间的转换方法。

主要实现代码如下:

public final class Byte extends Number implements Comparable<Byte> {

public static final byte   MIN_VALUE = -128;

public static final byte   MAX_VALUE = 127;

public static final int SIZE = 8;

public static final int BYTES = SIZE / Byte.SIZE;

private final byte value;

public static final Class<Byte>     TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");

public Byte(byte value) {
this.value = value;
}

public Byte(String s) throws NumberFormatException {
this.value = parseByte(s, 10);
}

public static String toString(byte b) {
return Integer.toString((int)b, 10);
}

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));
}
}

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);
}

public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}

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);
}

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);
}

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);
}

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;
}

public int compareTo(Byte anotherByte) {
return compare(this.value, anotherByte.value);
}

public static int compare(byte x, byte y) {
return x - y;
}

public static int toUnsignedInt(byte x) {
return ((int) x) & 0xff;
}

public static long toUnsignedLong(byte x) {
return ((long) x) & 0xffL;
}

}


变量

public static final byte   MIN_VALUE = -128;
public static final byte   MAX_VALUE = 127;
public static final int SIZE = 8;
public static final int BYTES = SIZE / Byte.SIZE;
private final byte value;
public static final Class<Byte>     TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");


MIN_VALUE
静态变量表示byte能取的最小值,值为-128,被final修饰说明不可变;

类似的还有
MAX_VALUE
,表示byte的最大值为127。

SIZE
用来表示于二进制补码形式的byte值的比特数,值为8,静态变量且不可变。

BYTES
用来表示于二进制补码形式的byte值的字节数,值为1,静态变量且不可变。

由于是对byte的封装,所以必定要有一个变量来保存byte的值,即
value
,同样它也被final修饰说明不可变。

TYPE
的toString的值是
byte


Class的
getPrimitiveClass
是一个native方法,在
Class.c
中有个
Java_java_lang_Class_getPrimitiveClass
方法与之对应,所以JVM层面会通过
JVM_FindPrimitiveClass
函数会根据”byte”字符串获得jclass,最终到Java层则为
Class<Byte>


JNIEXPORT jclass JNICALL
Java_java_lang_Class_getPrimitiveClass(JNIEnv *env,
jclass cls,
jstring name)
{
const char *utfName;
jclass result;

if (name == NULL) {
JNU_ThrowNullPointerException(env, 0);
return NULL;
}

utfName = (*env)->GetStringUTFChars(env, name, 0);
if (utfName == 0)
return NULL;

result = JVM_FindPrimitiveClass(env, utfName);

(*env)->ReleaseStringUTFChars(env, name, utfName);

return result;
}


TYPE
执行toString时,逻辑如下,则其实是
getName
函数决定其值,
getName
通过native方法
getName0
从JVM层获取名称,

public String toString() {
return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
+ getName();
}


getName0
根据一个数组获得对应的名称,JVM根据Java层的Class可得到对应类型的数组下标,比如这里下标为8,则名称为”byte”。

const char* type2name_tab[T_CONFLICT+1] = {
NULL, NULL, NULL, NULL,
"boolean",
"char",
"float",
"double",
"byte",
"short",
"int",
"long",
"object",
"array",
"void",
"*address*",
"*narrowoop*",
"*conflict*"
};


parseByte方法

两个parseByte方法,主要看第一个即可,第一个参数是待转换的字符串,第二个参数表示进制数,这里的转换其实是调了Integer的parseInt方法,返回值再判断是不是在byte的最小值和最大值之间。怎么更好理解这个参数呢?举个例子,
Byte.parseByte("100",10)
表示十进制的100,所以值为100,而
Byte.parseByte("100",2)
表示二进制的100,所以值为4。另外如果
Byte.parseByte("1000",10)
会抛出
java.lang.NumberFormatException
异常。

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和String类型。它是通过调用parseByte方法进行转换的,所以转换逻辑与上面的parseByte方法一样。

public Byte(byte value) {
this.value = value;
}

public Byte(String s) throws NumberFormatException {
this.value = parseByte(s, 10);
}


toString方法

一个是静态方法一个是非静态方法,但两个方法转换的效果是一样的,都是以十进制形式转换。

public static String toString(byte b) {
return Integer.toString((int)b, 10);
}
public String toString() {
return Integer.toString((int)value);
}


ByteCache内部类

ByteCache是Byte的一个内部类,它其实就是一个包含了byte所有可能值的Byte数组,对于byte来说其实它的可能值就是从-128到127,一共256个,所以我们只需要实例化256个Byte对象就可以表示所有可能的byte。而且这些都是静态且final的,避免重复的实例化和回收。

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));
}
}


valueOf方法

有三个valueOf方法,主要看下面这个,因为ByteCache包含了所有byte可能值的Byte对象,直接从ByteCache的数组中获取对应的Byte对象即可。

public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}


decode方法

decode方法主要作用是解码字符串转成Byte型,比如
Byte.decode("11")
的结果为11,而
Byte.decode("0x11")
结果为17,因为后面的是十六进制,它会根据实际情况进行解码。

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);
}


xxxValue方法

包括shortValue、intValue、longValue、floatValue和doubleValue等方法,其实就是转换成对应的类型。

hashCode方法

hashCode方法很简单,就是直接返回int类型的值。

public int hashCode() {
return Byte.hashCode(value);
}

public static int hashCode(byte value) {
return (int)value;
}


equals方法

比较是否相同时先判断是不是Byte类型再比较值。

public boolean equals(Object obj) {
if (obj instanceof Byte) {
return value == ((Byte)obj).byteValue();
}
return false;
}


compare方法

通过相减来比较,大于0则说明x大于y。

public static int compare(byte x, byte y) {
return x - y;
}


无符号转换

包括转成无符号int型和无符号long型。

public static int toUnsignedInt(byte x) {
return ((int) x) & 0xff;
}

public static long toUnsignedLong(byte x) {
return ((long) x) & 0xffL;
}


以下是广告相关阅读

========广告时间========

公众号的菜单已分为“分布式”、“机器学习”、“深度学习”、“NLP”、“Java深度”、“Java并发核心”、“JDK源码”、“Tomcat内核”等,可能有一款适合你的胃口。

鄙人的新书《Tomcat内核设计剖析》已经在京东销售了,有需要的朋友可以购买。感谢各位朋友。

为什么写《Tomcat内核设计剖析》

=========================

相关阅读:

从JDK源码角度看Object

谈谈Java基础数据类型

从JDK源码角度看并发锁的优化

从JDK源码角度看线程的阻塞和唤醒

从JDK源码角度看并发竞争的超时

从JDK源码角度看java并发线程的中断

从JDK源码角度看Java并发的公平性

从JDK源码角度看java并发的原子性如何保证

从JDK源码角度看Boolean

欢迎关注:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: