0
点赞
收藏
分享

微信扫一扫

JDK源码(8)-Byte


一、抽象类Number

类继承关系

JDK源码(8)-Byte_抽象方法

这里面的原子类、BigDecimal后面都会详细介绍。

属性和抽象方法

JDK源码(8)-Byte_抽象方法_02

二、概述

所有的属性,最小-128,最大127,SIZE和BYTES代码比特值的二进制补码长度。

JDK源码(8)-Byte_原子类_03

一个静态内部类做缓存

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

三、常见方法

compare

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

toUnsignedInt、toUnsignedLong

将一个byte转换为一个无符号的int值,跟0xff做与运算

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

valueOf,参数radix代表进制

public static Byte valueOf(String s, int radix)
throws NumberFormatException {
return valueOf(parseByte(s, radix));
}

decode:将字符串解码WieByte类型

支持二进制,八进制,十六进制,会调用Integer的decode方法进行处理,默认十进制,关于这个方法后面再讲。


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

hashCode就是该byte对应的int值

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

没有了,很简单的一个类。

 

举报

相关推荐

0 条评论