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

java源码分析(9)-Byte

2016-06-12 11:45 525 查看
Byte

1.Byte

Byte为final修饰不能继承,实现了comparable接口,可用于比较,同时继承了Number类,需要实现数字类型转换的一系列方法

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;//Byte用八位表示,即一个字节
public byte byteValue() {
<span style="white-space:pre">	</span>return value;
    }
    public short shortValue() {
<span style="white-space:pre">	</span>return (short)value;
    }
    public int intValue() {
<span style="white-space:pre">	</span>return (int)value;
    }
    public long longValue() {
<span style="white-space:pre">	</span>return (long)value;
    }
    public float floatValue() {
<span style="white-space:pre">	</span>return (float)value;
    }
    public double doubleValue() {
<span style="white-space:pre">	</span>return (double)value;
    }


2.构造器
只有以下两个构造器

public Byte(byte value) {
this.value = value;//传入的必须是Byte类型的值
}
public Byte(String s) throws NumberFormatException {
        this.value = parseByte(s, 10);//传入的必须是可以转换为数字的字符串
    }
3.缓存值
Byte的缓存值,-128~127总共256个值,在这个区间内,jvm会直接使用缓存值,当值超出这个区间时,会出现溢出现象,如128会等于-128,从最小值再次开始算值

private static class ByteCache {
private ByteCache(){}

static final Byte cache[] = new Byte[-(-128) + 127 + 1];//缓存数组的长度为256
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Byte((byte)(i - 128));//将-128~127从小到大缓存
}
}4.parseByte
public static byte parseByte(String s) throws NumberFormatException {
return parseByte(s, 10);
}
public static byte parseByte(String s, int radix)
throws NumberFormatException {
int i = Integer.parseInt(s, radix);//s需为-128~127的字符,不然下一步就会报错
if (i < MIN_VALUE || i > MAX_VALUE)
throw new NumberFormatException(
"Value out of range. Value:\"" + s + "\" Radix:" + radix);
return (byte)i;
}
5.decode
解码转码方法,将String转为Byte

public static Byte decode(String nm) throws NumberFormatException {
int radix = 10;
int index = 0;
boolean negative = false;
Byte result;
if (nm.startsWith("-")) {
negative = true;
index++;
}
//判定16进制数
if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
index += 2;
radix = 16;
} else if (nm.startsWith("#", index)) {
index++;
radix = 16;
} else if (nm.startsWith("0", index) && nm.length() > 1 + index) {//判定8进制数
index++;
radix = 8;
}
if (nm.startsWith("-", index))//位置不正确的负号
throw new NumberFormatException("Negative sign in wrong position");
try {
result = Byte.valueOf(nm.substring(index), radix);
result = negative ? new Byte((byte)-result.byteValue()) : result;
} catch (NumberFormatException e) {
String constant = negative ? new String("-" + nm.substring(index))
: nm.substring(index);
result = Byte.valueOf(constant, radix);
}
return result;
}6.toString,hashCode,equals
public String toString() {
return String.valueOf((int)value);//toString方法返回的是ACCII码的编码,而不是值本身,例如a的toString为97
}
public int hashCode() {
return (int)value;//hash值返回的是asc码的数字值
}
public boolean equals(Object obj) {
if (obj instanceof Byte) {
return value == ((Byte)obj).byteValue();//重写equals方法,比较value地址值,与==功能相同
}
return false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: