java.lang.Byte JDK1.6源码 (不含注释)
/*
* @(#)Byte.java 1.41 05/11/17
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.lang;
/**
* @author Nakul Saraiya
* @version 1.41, 11/17/05
* @see java.lang.Number
* @since JDK1.1
*/
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 Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
public static final int SIZE = 8;
private static final long serialVersionUID = -7183698231559129828L;
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 valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
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);
if (i < MIN_VALUE || i > MAX_VALUE)
throw new NumberFormatException(
"Value out of range. Value:\"" + s + "\" Radix:" + radix);
return (byte)i;
}
public static Byte valueOf(String s, int radix)throws NumberFormatException {
return new Byte(parseByte(s, radix));
}
public static Byte valueOf(String s) throws NumberFormatException {
return valueOf(s, 10);
}
public static Byte decode(String nm) throws NumberFormatException {
int radix = 10;
int index = 0;
boolean negative = false;
Byte result;
// Handle minus sign, if present
if (nm.startsWith("-")) {
negative = true;
index++;
}
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) {
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;
}
private final byte value;
public Byte(byte value) {
this.value = value;
}
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;
}
public String toString() {
return String.valueOf((int)value);
}
public int hashCode() {
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 this.value - anotherByte.value;
}
}
=========================================================注释===================================================================
package com.testjava.java.lang;
import java.lang.*;
/**
* this class for test java.lang.Boolean
* @author Administrator
* @Date 2012年2月15日
*/
public class TestByte {
public static byte MAX_VALUE;
public static byte MIN_VALUE;
public static byte SIZE;
public static Class<Byte> TYPE;
private final byte value;
Byte result;
//construction one
public TestByte(byte value) {
this.value = value;
}
//construction two
public TestByte(String s) throws NumberFormatException {
this.value = parseByte(s, 10);
}
/**
* 返回一个Byte类内置的私有最终对象value
* @return
*/
public byte byteValue() {
return value;
}
/**
* 返回一个Byte类型的byte值
* @param b
* @return
*/
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
/**
* 比较两个 byte 对象 返回一个int 类型的 值差 this - paramByte
* @param anotherByte
* @return
*/
public int comparaTo(Byte anotherByte) {
return this.value - anotherByte.byteValue();
}
/**
* 将 String解码为 byte
* @param nm
* @return
*/
public static Byte decode(String nm) {
//声明一个基数为:10进制
int radix = 10;
//声明一个遍历的入口位置
int index = 0;
//声明一个状态值表示正负
boolean negative = false;
//声明一个字节对象以备返回
Byte result;
//如果是负数,那么从第二位开始遍历
if (nm.startsWith("-")) {
negative = true;
index++;
}
//如果以 0X 0x开头为16进制 从第二位开始遍历 基数为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) {
index ++;
radix = 8;
}
if (nm.startsWith("-", index)) {
throw new NumberFormatException("Negative sign in wrong postation!");
}
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;
}
/**
* 将byte类型强制转换为double类型
* @return
*/
public double doubleValue() {
return (double)value;
}
/**
* 判断一个对象是否为byte类型如果是则返回该对象强制转换后的值,否则返回false
* @param obj
* @return
*/
public boolean equlas(Object obj) {
if (obj instanceof Byte) {
return value == ((Byte)obj).byteValue();
}
return false;
}
/**
* 返回带两个参数parseByte(String s, int radix)方法的重载
* @param s
* @return
*/
public static byte parseByte(String s) {
return parseByte(s, 10);
}
/**
* 将String类型转换为byte类型
* @param s
* @param radix
* @return
* @throws 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;
}
/**
* 私有内部类,直接调用Byte缓存(在内存中直接生成byte减少New)来提高效率
* @author Administrator
*
*/
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));
}
}
/**
* 返回一个被强转为float类型的byte类型的对象
* @return
*/
public float floatValue() {
return (float)value;
}
/**
*
* JDK:返回此byte的hashCode编码
*/
public int hashCode() {
return (int)value;
}
/**
* 返回一个被强转为int类型的byte类型的对象
* @return
*/
public int intValue() {
return (int)value;
}
/**
* 返回一个被强转为long类型的byte类型的对象
* @return
*/
public long longValue() {
return (long)value;
}
/**
* 返回一个被强转为short类型的byte类型的对象
* @return
*/
public short shortValue() {
return (short)value;
}
/**
* 返回一个String类型(调用Integer类型toString(int b, int radix)方法的)的对象
* @param b
* @return
*/
public String toString(byte b) {
return Integer.toString((int)b, 10);
}
/**
* 返回一个String类型(强转为int类型的byte对象)的对象
*/
public String toString() {
return String.valueOf((int)value);
}
/**
* 返回一个byte类型的(强转为int类型后跟byte范围做比较如果符合则再强转回byte类型)的对象
* @param s
* @param radix
* @return
* @throws NumberFormatException
*/
public static Byte valueOf(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;
}
/**
* 返回一个byte类型的(调用重载方法valueOf(String s, int radix))的对象
* @param s
* @return
*/
public static Byte valueOf(String s) {
return valueOf(s, 10);
}
/**
* main
* @param args
*/
public static void main(String[] args) {
byte a = 1;
byte b = 10;
String c = "-129";
TestByte tb = new TestByte(a);
System.out.println(tb.hashCode());
System.out.println(new Byte(b).hashCode());
System.out.println(new String("abc").hashCode());
System.out.println(valueOf(c, 10));
}
}