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> {
2.构造器
只有以下两个构造器
public Byte(byte value) {
this.value = value;//传入的必须是Byte类型的值
}
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
解码转码方法,将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;
}
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;
}
相关文章推荐
- Java文件上传下载、邮件收发实例代码
- spring securit(五)
- Java中使用json与前台Ajax数据交互的方法
- java基础语法练习--汉诺塔问题
- Struts2学习笔记(4)-通配符的使用
- spring security(四)
- springMVC ResponseBody 返回汉字乱码解决方案
- java 区分error和exception
- java对象创建的过程总结
- JAVA设计模式之单例模式
- spring security(三)
- java基础语法练习--求出两数的最大公约数和最小公倍数
- 2016 java web 期末大作业心得体会 -- 小型的新闻发布系统(承认自己做的很烂)
- java 日期加减计算 获取上周、本周、下周的时间
- Spring Security(二)
- Java 自定义 线程池与任务队列
- 使用Java如何让需求在指定时间内生效
- logic:iterator struts1 功能小例 (转)
- Java框架SSH结合Easyui控件实现省市县三级联动示例解析
- Eclipse快捷键