Java 源码分析(三)
来源:互联网 发布:手机抢号软件 编辑:程序博客网 时间:2024/06/05 02:26
二、Byte类
Byte类跟Integer类大同小异,很多是调用Integer类的方法,要注意的是,Byte类也有缓存机制,范围也是-128-127,这里只贴一下源码,不做过多解释。
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 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, 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(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); } 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 Integer.toString((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 compare(this.value, anotherByte.value); } public static int compare(byte x, byte y) { return x - y; } public static final int SIZE = 8;}
三、Short类
同理。
public final class Short extends Number implements Comparable<Short> { public static final short MIN_VALUE = -32768; public static final short MAX_VALUE = 32767; public static final Class<Short> TYPE = (Class<Short>) Class.getPrimitiveClass("short"); public static String toString(short s) { return Integer.toString((int)s, 10); } public static short parseShort(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 (short)i; } public static short parseShort(String s) throws NumberFormatException { return parseShort(s, 10); } public static Short valueOf(String s, int radix) throws NumberFormatException { return valueOf(parseShort(s, radix)); } public static Short valueOf(String s) throws NumberFormatException { return valueOf(s, 10); } private static class ShortCache { private ShortCache(){} static final Short cache[] = new Short[-(-128) + 127 + 1]; static { for(int i = 0; i < cache.length; i++) cache[i] = new Short((short)(i - 128)); } } public static Short valueOf(short s) { final int offset = 128; int sAsInt = s; if (sAsInt >= -128 && sAsInt <= 127) { return ShortCache.cache[sAsInt + offset]; } return new Short(s); } public static Short 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((short)i); } private final short value; public Short(short value) { this.value = value; } public Short(String s) throws NumberFormatException { this.value = parseShort(s, 10); } public byte byteValue() { return (byte)value; } public short shortValue() { return 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 (int)value; } public boolean equals(Object obj) { if (obj instanceof Short) { return value == ((Short)obj).shortValue(); } return false; } public int compareTo(Short anotherShort) { return compare(this.value, anotherShort.value); } public static int compare(short x, short y) { return x - y; } public static final int SIZE = 16; public static short reverseBytes(short i) { return (short) (((i & 0xFF00) >> 8) | (i << 8)); }}
四、Long类
再同理。
public final class Short extends Number implements Comparable<Short> { public static final short MIN_VALUE = -32768; public static final short MAX_VALUE = 32767; public static final Class<Short> TYPE = (Class<Short>) Class.getPrimitiveClass("short"); public static String toString(short s) { return Integer.toString((int)s, 10); } public static short parseShort(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 (short)i; } public static short parseShort(String s) throws NumberFormatException { return parseShort(s, 10); } public static Short valueOf(String s, int radix) throws NumberFormatException { return valueOf(parseShort(s, radix)); } public static Short valueOf(String s) throws NumberFormatException { return valueOf(s, 10); } private static class ShortCache { private ShortCache(){} static final Short cache[] = new Short[-(-128) + 127 + 1]; static { for(int i = 0; i < cache.length; i++) cache[i] = new Short((short)(i - 128)); } } public static Short valueOf(short s) { final int offset = 128; int sAsInt = s; if (sAsInt >= -128 && sAsInt <= 127) { return ShortCache.cache[sAsInt + offset]; } return new Short(s); } public static Short 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((short)i); } private final short value; public Short(short value) { this.value = value; } public Short(String s) throws NumberFormatException { this.value = parseShort(s, 10); } public byte byteValue() { return (byte)value; } public short shortValue() { return 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 (int)value; } public boolean equals(Object obj) { if (obj instanceof Short) { return value == ((Short)obj).shortValue(); } return false; } public int compareTo(Short anotherShort) { return compare(this.value, anotherShort.value); } public static int compare(short x, short y) { return x - y; } public static final int SIZE = 16; public static short reverseBytes(short i) { return (short) (((i & 0xFF00) >> 8) | (i << 8)); }}
五、Boolean类
public final class Boolean implements java.io.Serializable, Comparable<Boolean>{ public static final Boolean TRUE = new Boolean(true); public static final Boolean FALSE = new Boolean(false); public static final Class<Boolean> TYPE = Class.getPrimitiveClass("boolean"); private final boolean value; public Boolean(boolean value) { this.value = value; } public Boolean(String s) { this(toBoolean(s)); } public static boolean parseBoolean(String s) { return toBoolean(s); } public boolean booleanValue() { return value; } public static Boolean valueOf(boolean b) { return (b ? TRUE : FALSE); } public static Boolean valueOf(String s) { return toBoolean(s) ? TRUE : FALSE; } public static String toString(boolean b) { return b ? "true" : "false"; } public String toString() { return value ? "true" : "false"; } public int hashCode() { return value ? 1231 : 1237; } public boolean equals(Object obj) { if (obj instanceof Boolean) { return value == ((Boolean)obj).booleanValue(); } return false; } public static boolean getBoolean(String name) { boolean result = false; try { result = toBoolean(System.getProperty(name)); } catch (IllegalArgumentException e) { } catch (NullPointerException e) { } return result; } public int compareTo(Boolean b) { return compare(this.value, b.value); } public static int compare(boolean x, boolean y) { return (x == y) ? 0 : (x ? 1 : -1); } private static boolean toBoolean(String name) { return ((name != null) && name.equalsIgnoreCase("true")); }}
1)value成员
private final boolean value;
Boolean类的值也是储存在value中。
2)几个常量
public static final Boolean TRUE = new Boolean(true);public static final Boolean FALSE = new Boolean(false);
要注意,Boolean类的常量是Boolean类型而不是boolean。
3)构造方法
public Boolean(boolean value) {this.value = value;}public Boolean(String s) {this(toBoolean(s));}
看一下第二个构造方法,用字符串构造,调用了toBoolean()方法,接下来先看看这是个怎样的方法。
private static boolean toBoolean(String name) {return ((name != null) && name.equalsIgnoreCase("true"));}
可以看到,如果字符串不为空并且为“true”(忽略大小写),返回值为true,否则,字符串为空或是其余任何字符串,返回值都是false。之后很多方法都调用了这个方法。
4)hashCode()方法
public int hashCode() {return value ? 1231 : 1237;}
如果为true,返回1231,如果为false,返回1237。
5)compareTo()方法
public int compareTo(Boolean b) {return compare(this.value, b.value);}public static int compare(boolean x, boolean y) {return (x == y) ? 0 : (x ? 1 : -1);}
根据这个compare()可以这样认为,真大于假。
阅读全文
1 0
- Java 源码分析(三)
- Java容器(三):LinkedList源码分析
- 【Java】【Flume】Flume-NG启动过程源码分析(三)
- 【Java】【Flume】Flume-NG启动过程源码分析(三)
- java 中的集合(三) ArrayList源码分析
- Java集合源码学习(三)LinkedList分析
- JUnit源码分析(三)
- Log4net源码分析(三)
- Log4net源码分析(三)
- Logcat源码分析(三)
- Log4net源码分析(三)
- pomelo源码分析(三)
- H264源码分析(三)
- mosquitto源码分析(三)
- openMPM源码分析(三)
- mosquitto源码分析(三)
- MooseFS源码分析(三)
- Tomcat8源码分析(三)
- 2017第十七届中国(北京)国际食品及高端饮品展览会 会刊(参展商名录)
- 排序算法专项总结
- Ubuntu安装QIIME2-2017.8版本
- iOS绘图教程
- 棋盘问题(分治)
- Java 源码分析(三)
- visibility和display
- 【LeetCode】Permutation II(带重复元素的排列)
- Hibernate 第一个hibernate程序
- POJ总结(3094)
- 【bzoj3231】[Sdoi2008]递归数列(矩乘)
- Hibernate 配置详解
- Power Calculus UVA
- 1026程序运行时间(模拟)