java中封装类(二)
java中的数字类型包括 Byte,Short,Integer,Long,Float,Double.其中前四个是整数,后两个是浮点数。 在说java中数字类型之前先来看看它们统一的基类Number。
package java.lang; public abstract class Number implements java.io.Serializable { public abstract int intValue(); public abstract long longValue(); public abstract float floatValue(); public abstract double doubleValue(); public byte byteValue() { return (byte)intValue(); } public short shortValue() { return (short)intValue(); } private static final long serialVersionUID = -8742448824652078965L; }
代码并不复杂,从代码可以看出数字类型的封装类可以转化为任意数字类型,也就是说数字类型是可以相互转化的。
首先概括的说一下这六个类型都是数字类型,都包含了这四个静态字段,分别是
Size代表数据二进制位的长度,MAX_VALUE最大值,MIN_VALUE最小值,和TYPE对应的基元类型的Class
需要特别说明的是 TYPE虽然也是Class<Long>,但是它和 Long.class的Class不是同一个对象。因为java的泛型是伪泛型,<>中的类型,只是代表该类型的对象可以强转为<>中的类型。
package demo.nio; public class NumberDemo { public static void main(String[] args) { System.out.println(Long.class.getName());//java.lang.Long System.out.println(Long.TYPE.getName());//long } }
四个整型类型的数据中,Byte的Size是8,也就是一个字节,Short的SIZE是16,也就是两个字节,Integer是32也就是4个字节,Long的SIZE是64也就是8个字节,它们的最大,最小值随长度的大小而变化,长度越大,最大值越大,最小值越小。
package demo.nio; public class NumberDemo { public static void main(String[] args) { System.out.println(Byte.MAX_VALUE);//127 System.out.println(Byte.MIN_VALUE);//-128 System.out.println(Short.MAX_VALUE);//32767 System.out.println(Short.MIN_VALUE);//-32768 System.out.println(Integer.MAX_VALUE);//2147483647 System.out.println(Integer.MIN_VALUE);//-2147483648 System.out.println(Long.MAX_VALUE);//9223372036854775807 System.out.println(Long.MIN_VALUE);//-9223372036854775808 } }
先说Byte类型,Byte就是一个字节,代表8个二进制位。内部有一个value字段,用于存放装箱前的数据。而这个封装类的hashCode就是该value,数字类型都包含这样几个静态函数:
valueOf(XXX v)
valueOf(String s, int radix)
valueOf(String s)
parseXXX(String s)
parseXXX(String s, int radix)
decode(String nm)
其中Byte的parse和decode都是调用Integer的方法实现的。其中parse用于转化普通数字,decode用于转化 0xa之类的数字。
package java.lang; 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; private static final long serialVersionUID = -7183698231559129828L; }
因为数字类型都实现了Comparable接口,也就是说他是可比较的类型,对于Byte compare的实现就是简单的减去,除此之外还有个ByteCache,存放了-128~127的装箱类型的数组。
valueOf(byte b)返回的就是Cache中的数据。
package demo.nio; public class NumberDemo { public static void main(String[] args) { System.out.println(Byte.valueOf((byte) 10)); System.out.println(Byte.valueOf("10")); System.out.println(Byte.valueOf("10",10)); System.out.println(Byte.parseByte("a",16)); System.out.println(Byte.parseByte("10")); System.out.println(Byte.decode("10")); System.out.println(Byte.decode("0xa"));//10 } }
Short和Byte类型基本相同,valueOf,parseXXX,decode也是调用的Integer实现的,不过这里多了一个,reverseBytes(short i)函数,该函数的功能是将short数据反转,就是后八位变成前8位,前八位变成后八位。
如:0x100,反转之后就是0x1,0xa00反转之后就是10
Integer类型相对于前边的两个数字类型就复杂的多
首先它实现了parseXXX函数,toString函数,其次它多了很多静态函数。
package java.lang; import java.util.Properties; public final class Integer extends Number implements Comparable<Integer> { public static final int MIN_VALUE = 0x80000000; public static final int MAX_VALUE = 0x7fffffff; public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int"); final static char[] digits = { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' }; public static String toString(int i, int radix) { if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) radix = 10; /* Use the faster version */ if (radix == 10) { return toString(i); } char buf[] = new char[33]; boolean negative = (i < 0); int charPos = 32; if (!negative) { i = -i; } while (i <= -radix) { buf[charPos--] = digits[-(i % radix)]; i = i / radix; } buf[charPos] = digits[-i]; if (negative) { buf[--charPos] = '-'; } return new String(buf, charPos, (33 - charPos)); } public static String toHexString(int i) { return toUnsignedString(i, 4); } public static String toOctalString(int i) { return toUnsignedString(i, 3); } public static String toBinaryString(int i) { return toUnsignedString(i, 1); } /** * Convert the integer to an unsigned number. */ private static String toUnsignedString(int i, int shift) { char[] buf = new char[32]; int charPos = 32; int radix = 1 << shift; int mask = radix - 1; do { buf[--charPos] = digits[i & mask]; i >>>= shift; } while (i != 0); return new String(buf, charPos, (32 - charPos)); } final static char [] DigitTens = { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '9', '9', '9', '9', '9', '9', '9', '9', '9', '9', } ; final static char [] DigitOnes = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', } ; public static String toString(int i) { if (i == Integer.MIN_VALUE) return "-2147483648"; int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); char[] buf = new char[size]; getChars(i, size, buf); return new String(buf, true); } static void getChars(int i, int index, char[] buf) { int q, r; int charPos = index; char sign = 0; if (i < 0) { sign = '-'; i = -i; } // Generate two digits per iteration while (i >= 65536) { q = i / 100; // really: r = i - (q * 100); r = i - ((q << 6) + (q << 5) + (q << 2)); i = q; buf [--charPos] = DigitOnes[r]; buf [--charPos] = DigitTens[r]; } // Fall thru to fast mode for smaller numbers // assert(i <= 65536, i); for (;;) { q = (i * 52429) >>> (16+3); r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ... buf [--charPos] = digits [r]; i = q; if (i == 0) break; } if (sign != 0) { buf [--charPos] = sign; } } final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, Integer.MAX_VALUE }; // Requires positive x static int stringSize(int x) { for (int i=0; ; i++) if (x <= sizeTable[i]) return i+1; } public static int parseInt(String s, int radix)throws NumberFormatException{ /* * WARNING: This method may be invoked early during VM initialization * before IntegerCache is initialized. Care must be taken to not use * the valueOf method. */ if (s == null) { throw new NumberFormatException("null"); } if (radix < Character.MIN_RADIX) { throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX"); } if (radix > Character.MAX_RADIX) { throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX"); } int result = 0; boolean negative = false; int i = 0, len = s.length(); int limit = -Integer.MAX_VALUE; int multmin; int digit; if (len > 0) { char firstChar = s.charAt(0); if (firstChar < '0') { // Possible leading "+" or "-" if (firstChar == '-') { negative = true; limit = Integer.MIN_VALUE; } else if (firstChar != '+') throw NumberFormatException.forInputString(s); if (len == 1) // Cannot have lone "+" or "-" throw NumberFormatException.forInputString(s); i++; } multmin = limit / radix; while (i < len) { // Accumulating negatively avoids surprises near MAX_VALUE digit = Character.digit(s.charAt(i++),radix); if (digit < 0) { throw NumberFormatException.forInputString(s); } if (result < multmin) { throw NumberFormatException.forInputString(s); } result *= radix; if (result < limit + digit) { throw NumberFormatException.forInputString(s); } result -= digit; } } else { throw NumberFormatException.forInputString(s); } return negative ? result : -result; } public static int parseInt(String s) throws NumberFormatException { return parseInt(s,10); } public static Integer valueOf(String s, int radix) throws NumberFormatException { return Integer.valueOf(parseInt(s,radix)); } public static Integer valueOf(String s) throws NumberFormatException { return Integer.valueOf(parseInt(s, 10)); } private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; static { // high value may be configured by property int h = 127; String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) { int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low)); } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); } private IntegerCache() {} } public static Integer valueOf(int i) { assert IntegerCache.high >= 127; if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); } private final int value; public Integer(int value) { this.value = value; } public Integer(String s) throws NumberFormatException { this.value = parseInt(s, 10); } public byte byteValue() { return (byte)value; } public short shortValue() { return (short)value; } public int intValue() { return value; } public long longValue() { return (long)value; } public float floatValue() { return (float)value; } public double doubleValue() { return (double)value; } public String toString() { return toString(value); } public int hashCode() { return value; } public boolean equals(Object obj) { if (obj instanceof Integer) { return value == ((Integer)obj).intValue(); } return false; } public static Integer getInteger(String nm) { return getInteger(nm, null); } public static Integer getInteger(String nm, int val) { Integer result = getInteger(nm, null); return (result == null) ? Integer.valueOf(val) : result; } public static Integer getInteger(String nm, Integer val) { String v = null; try { v = System.getProperty(nm); } catch (IllegalArgumentException e) { } catch (NullPointerException e) { } if (v != null) { try { return Integer.decode(v); } catch (NumberFormatException e) { } } return val; } public static Integer decode(String nm) throws NumberFormatException { int radix = 10; int index = 0; boolean negative = false; Integer result; if (nm.length() == 0) throw new NumberFormatException("Zero length string"); char firstChar = nm.charAt(0); // Handle sign, if present if (firstChar == '-') { negative = true; index++; } else if (firstChar == '+') index++; // Handle radix specifier, if present 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) || nm.startsWith("+", index)) throw new NumberFormatException("Sign character in wrong position"); try { result = Integer.valueOf(nm.substring(index), radix); result = negative ? Integer.valueOf(-result.intValue()) : result; } catch (NumberFormatException e) { // If number is Integer.MIN_VALUE, we'll end up here. The next line // handles this case, and causes any genuine format error to be // rethrown. String constant = negative ? ("-" + nm.substring(index)) : nm.substring(index); result = Integer.valueOf(constant, radix); } return result; } public int compareTo(Integer anotherInteger) { return compare(this.value, anotherInteger.value); } public static int compare(int x, int y) { return (x < y) ? -1 : ((x == y) ? 0 : 1); } public static final int SIZE = 32; public static int highestOneBit(int i) { // HD, Figure 3-1 i |= (i >> 1); i |= (i >> 2); i |= (i >> 4); i |= (i >> 8); i |= (i >> 16); return i - (i >>> 1); } public static int lowestOneBit(int i) { // HD, Section 2-1 return i & -i; } public static int numberOfLeadingZeros(int i) { // HD, Figure 5-6 if (i == 0) return 32; int n = 1; if (i >>> 16 == 0) { n += 16; i <<= 16; } if (i >>> 24 == 0) { n += 8; i <<= 8; } if (i >>> 28 == 0) { n += 4; i <<= 4; } if (i >>> 30 == 0) { n += 2; i <<= 2; } n -= i >>> 31; return n; } public static int numberOfTrailingZeros(int i) { // HD, Figure 5-14 int y; if (i == 0) return 32; int n = 31; y = i <<16; if (y != 0) { n = n -16; i = y; } y = i << 8; if (y != 0) { n = n - 8; i = y; } y = i << 4; if (y != 0) { n = n - 4; i = y; } y = i << 2; if (y != 0) { n = n - 2; i = y; } return n - ((i << 1) >>> 31); } public static int bitCount(int i) { // HD, Figure 5-2 i = i - ((i >>> 1) & 0x55555555); i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); i = (i + (i >>> 4)) & 0x0f0f0f0f; i = i + (i >>> 8); i = i + (i >>> 16); return i & 0x3f; } public static int rotateLeft(int i, int distance) { return (i << distance) | (i >>> -distance); } public static int rotateRight(int i, int distance) { return (i >>> distance) | (i << -distance); } public static int reverse(int i) { // HD, Figure 7-1 i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555; i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333; i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f; i = (i << 24) | ((i & 0xff00) << 8) | ((i >>> 8) & 0xff00) | (i >>> 24); return i; } public static int signum(int i) { // HD, Section 2-7 return (i >> 31) | (-i >>> 31); } public static int reverseBytes(int i) { return ((i >>> 24) ) | ((i >> 8) & 0xFF00) | ((i << 8) & 0xFF0000) | ((i << 24)); } /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = 1360826667806852920L; }
compare的实现是如果大于返回1,小于返回-1而不是简单的减。hashCode也是内部的value字段。而它的Cache就没有维护全部的数据而是-128-127,valueOf函数的操作是如果在这个范围内则返回cache中的值,否则new一个返回。
它的toString系列,比较复杂,toHexString 16进制 toOctalString 8进制 toBinaryString 2进制 toString转化为十进制,他还有一个重载版本toString(int i, int radix) 转化为指定进制。
parseXXX 系列 parseInt(String s)转化为10进制数据,parseInt(String s, int radix)转化为指定进制
highestOneBit,lowestOneBit 只保留最高位的1和最低位1的结果。
bitCount 二进制数据1的个数
numberOfLeadingZeros,numberOfTrailingZeros 开头和末尾0的个数
signum 如果大于0,返回1,小于返回-1,等于0返回0
reverseBytes反转数字,高位变低位,4到1位,3到二位,2到三位,1到四位
package demo.nio; public class NumberDemo { public static void main(String[] args) { System.out.println(Integer.toBinaryString(255)); System.out.println(Integer.toHexString(255)); System.out.println(Integer.toOctalString(255)); System.out.println(Integer.highestOneBit(-8));//-2147483648 System.out.println(Integer.bitCount(255));//8 System.out.println(Integer.lowestOneBit(-8));//8 System.out.println(Integer.numberOfLeadingZeros(-8));//0 System.out.println(Integer.numberOfTrailingZeros(-8));//3 System.out.println(Integer.signum(100));//1 System.out.println(Integer.signum(-100));//-1 System.out.println(Integer.signum(0));//0 System.out.println(Integer.toBinaryString(128)); System.out.println(Integer.toBinaryString(Integer.reverseBytes(128))); System.out.println(Integer.reverseBytes(128));//0 } }
Long和Integer功能就比较类似,只是因为Long是64位,所以所有的函数都要重新实现。
package java.lang; public final class Long extends Number implements Comparable<Long> { public static final long MIN_VALUE = 0x8000000000000000L; public static final long MAX_VALUE = 0x7fffffffffffffffL; public static final Class<Long> TYPE = (Class<Long>) Class.getPrimitiveClass("long"); public static String toString(long i, int radix) { if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) radix = 10; if (radix == 10) return toString(i); char[] buf = new char[65]; int charPos = 64; boolean negative = (i < 0); if (!negative) { i = -i; } while (i <= -radix) { buf[charPos--] = Integer.digits[(int)(-(i % radix))]; i = i / radix; } buf[charPos] = Integer.digits[(int)(-i)]; if (negative) { buf[--charPos] = '-'; } return new String(buf, charPos, (65 - charPos)); } public static String toHexString(long i) { return toUnsignedString(i, 4); } public static String toOctalString(long i) { return toUnsignedString(i, 3); } public static String toBinaryString(long i) { return toUnsignedString(i, 1); } private static String toUnsignedString(long i, int shift) { char[] buf = new char[64]; int charPos = 64; int radix = 1 << shift; long mask = radix - 1; do { buf[--charPos] = Integer.digits[(int)(i & mask)]; i >>>= shift; } while (i != 0); return new String(buf, charPos, (64 - charPos)); } public static String toString(long i) { if (i == Long.MIN_VALUE) return "-9223372036854775808"; int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); char[] buf = new char[size]; getChars(i, size, buf); return new String(buf, true); } static void getChars(long i, int index, char[] buf) { long q; int r; int charPos = index; char sign = 0; if (i < 0) { sign = '-'; i = -i; } // Get 2 digits/iteration using longs until quotient fits into an int while (i > Integer.MAX_VALUE) { q = i / 100; // really: r = i - (q * 100); r = (int)(i - ((q << 6) + (q << 5) + (q << 2))); i = q; buf[--charPos] = Integer.DigitOnes[r]; buf[--charPos] = Integer.DigitTens[r]; } // Get 2 digits/iteration using ints int q2; int i2 = (int)i; while (i2 >= 65536) { q2 = i2 / 100; // really: r = i2 - (q * 100); r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2)); i2 = q2; buf[--charPos] = Integer.DigitOnes[r]; buf[--charPos] = Integer.DigitTens[r]; } // Fall thru to fast mode for smaller numbers // assert(i2 <= 65536, i2); for (;;) { q2 = (i2 * 52429) >>> (16+3); r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ... buf[--charPos] = Integer.digits[r]; i2 = q2; if (i2 == 0) break; } if (sign != 0) { buf[--charPos] = sign; } } // Requires positive x static int stringSize(long x) { long p = 10; for (int i=1; i<19; i++) { if (x < p) return i; p = 10*p; } return 19; } public static long parseLong(String s, int radix)throws NumberFormatException{ if (s == null) { throw new NumberFormatException("null"); } if (radix < Character.MIN_RADIX) { throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX"); } if (radix > Character.MAX_RADIX) { throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX"); } long result = 0; boolean negative = false; int i = 0, len = s.length(); long limit = -Long.MAX_VALUE; long multmin; int digit; if (len > 0) { char firstChar = s.charAt(0); if (firstChar < '0') { // Possible leading "+" or "-" if (firstChar == '-') { negative = true; limit = Long.MIN_VALUE; } else if (firstChar != '+') throw NumberFormatException.forInputString(s); if (len == 1) // Cannot have lone "+" or "-" throw NumberFormatException.forInputString(s); i++; } multmin = limit / radix; while (i < len) { // Accumulating negatively avoids surprises near MAX_VALUE digit = Character.digit(s.charAt(i++),radix); if (digit < 0) { throw NumberFormatException.forInputString(s); } if (result < multmin) { throw NumberFormatException.forInputString(s); } result *= radix; if (result < limit + digit) { throw NumberFormatException.forInputString(s); } result -= digit; } } else { throw NumberFormatException.forInputString(s); } return negative ? result : -result; } public static long parseLong(String s) throws NumberFormatException { return parseLong(s, 10); } public static Long valueOf(String s, int radix) throws NumberFormatException { return Long.valueOf(parseLong(s, radix)); } public static Long valueOf(String s) throws NumberFormatException { return Long.valueOf(parseLong(s, 10)); } private static class LongCache { private LongCache(){} static final Long cache[] = new Long[-(-128) + 127 + 1]; static { for(int i = 0; i < cache.length; i++) cache[i] = new Long(i - 128); } } public static Long valueOf(long l) { final int offset = 128; if (l >= -128 && l <= 127) { // will cache return LongCache.cache[(int)l + offset]; } return new Long(l); } public static Long decode(String nm) throws NumberFormatException { int radix = 10; int index = 0; boolean negative = false; Long result; if (nm.length() == 0) throw new NumberFormatException("Zero length string"); char firstChar = nm.charAt(0); // Handle sign, if present if (firstChar == '-') { negative = true; index++; } else if (firstChar == '+') index++; // Handle radix specifier, if present 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) || nm.startsWith("+", index)) throw new NumberFormatException("Sign character in wrong position"); try { result = Long.valueOf(nm.substring(index), radix); result = negative ? Long.valueOf(-result.longValue()) : result; } catch (NumberFormatException e) { // If number is Long.MIN_VALUE, we'll end up here. The next line // handles this case, and causes any genuine format error to be // rethrown. String constant = negative ? ("-" + nm.substring(index)) : nm.substring(index); result = Long.valueOf(constant, radix); } return result; } private final long value; public Long(long value) { this.value = value; } public Long(String s) throws NumberFormatException { this.value = parseLong(s, 10); } public byte byteValue() { return (byte)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 toString(value); } public int hashCode() { return (int)(value ^ (value >>> 32)); } public boolean equals(Object obj) { if (obj instanceof Long) { return value == ((Long)obj).longValue(); } return false; } public static Long getLong(String nm) { return getLong(nm, null); } public static Long getLong(String nm, long val) { Long result = Long.getLong(nm, null); return (result == null) ? Long.valueOf(val) : result; } public static Long getLong(String nm, Long val) { String v = null; try { v = System.getProperty(nm); } catch (IllegalArgumentException e) { } catch (NullPointerException e) { } if (v != null) { try { return Long.decode(v); } catch (NumberFormatException e) { } } return val; } public int compareTo(Long anotherLong) { return compare(this.value, anotherLong.value); } public static int compare(long x, long y) { return (x < y) ? -1 : ((x == y) ? 0 : 1); } public static final int SIZE = 64; public static long highestOneBit(long i) { // HD, Figure 3-1 i |= (i >> 1); i |= (i >> 2); i |= (i >> 4); i |= (i >> 8); i |= (i >> 16); i |= (i >> 32); return i - (i >>> 1); } public static long lowestOneBit(long i) { // HD, Section 2-1 return i & -i; } public static int numberOfLeadingZeros(long i) { // HD, Figure 5-6 if (i == 0) return 64; int n = 1; int x = (int)(i >>> 32); if (x == 0) { n += 32; x = (int)i; } if (x >>> 16 == 0) { n += 16; x <<= 16; } if (x >>> 24 == 0) { n += 8; x <<= 8; } if (x >>> 28 == 0) { n += 4; x <<= 4; } if (x >>> 30 == 0) { n += 2; x <<= 2; } n -= x >>> 31; return n; } public static int numberOfTrailingZeros(long i) { // HD, Figure 5-14 int x, y; if (i == 0) return 64; int n = 63; y = (int)i; if (y != 0) { n = n -32; x = y; } else x = (int)(i>>>32); y = x <<16; if (y != 0) { n = n -16; x = y; } y = x << 8; if (y != 0) { n = n - 8; x = y; } y = x << 4; if (y != 0) { n = n - 4; x = y; } y = x << 2; if (y != 0) { n = n - 2; x = y; } return n - ((x << 1) >>> 31); } public static int bitCount(long i) { // HD, Figure 5-14 i = i - ((i >>> 1) & 0x5555555555555555L); i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L); i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL; i = i + (i >>> 8); i = i + (i >>> 16); i = i + (i >>> 32); return (int)i & 0x7f; } public static long rotateLeft(long i, int distance) { return (i << distance) | (i >>> -distance); } public static long rotateRight(long i, int distance) { return (i >>> distance) | (i << -distance); } public static long reverse(long i) { // HD, Figure 7-1 i = (i & 0x5555555555555555L) << 1 | (i >>> 1) & 0x5555555555555555L; i = (i & 0x3333333333333333L) << 2 | (i >>> 2) & 0x3333333333333333L; i = (i & 0x0f0f0f0f0f0f0f0fL) << 4 | (i >>> 4) & 0x0f0f0f0f0f0f0f0fL; i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL; i = (i << 48) | ((i & 0xffff0000L) << 16) | ((i >>> 16) & 0xffff0000L) | (i >>> 48); return i; } public static int signum(long i) { // HD, Section 2-7 return (int) ((i >> 63) | (-i >>> 63)); } public static long reverseBytes(long i) { i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL; return (i << 48) | ((i & 0xffff0000L) << 16) | ((i >>> 16) & 0xffff0000L) | (i >>> 48); } /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = 4290774380558885855L; }
Float,Double是浮点数,相较于整数就复杂的多,除了整数中几个静态字段外,它又多了很多静态字段
POSITIVE_INFINITY 正无穷 NEGATIVE_INFINITY 负无穷 NaN 非数字 MAX_EXPONENT 最大指数 MIN_EXPONENT 最小指数 NAN 非数字
所以浮点数都有两个函数,判断是否无穷大,是否非数字
Float的Size是32,Double是64
它的compare值得一提,public static native int floatToRawIntBits(float value); 是通过这个本地函数,转化为int来比较大小的。parseFloat和valueOf系列调用的是另一个包里的函数,
toString系列是调用Double小的toString
package java.lang; import sun.misc.FloatingDecimal; import sun.misc.FpUtils; import sun.misc.FloatConsts; import sun.misc.DoubleConsts; public final class Float extends Number implements Comparable<Float> { public static final float POSITIVE_INFINITY = 1.0f / 0.0f; public static final float NEGATIVE_INFINITY = -1.0f / 0.0f; public static final float NaN = 0.0f / 0.0f; public static final float MAX_VALUE = 0x1.fffffeP+127f; // 3.4028235e+38f public static final float MIN_NORMAL = 0x1.0p-126f; // 1.17549435E-38f public static final float MIN_VALUE = 0x0.000002P-126f; // 1.4e-45f public static final int MAX_EXPONENT = 127; public static final int MIN_EXPONENT = -126; public static final int SIZE = 32; public static final Class<Float> TYPE = Class.getPrimitiveClass("float"); public static String toString(float f) { return new FloatingDecimal(f).toJavaFormatString(); } public static String toHexString(float f) { if (Math.abs(f) < FloatConsts.MIN_NORMAL && f != 0.0f ) {// float subnormal String s = Double.toHexString(FpUtils.scalb((double)f, /* -1022+126 */ DoubleConsts.MIN_EXPONENT- FloatConsts.MIN_EXPONENT)); return s.replaceFirst("p-1022$", "p-126"); } else // double string will be the same as float string return Double.toHexString(f); } public static Float valueOf(String s) throws NumberFormatException { return new Float(FloatingDecimal.readJavaFormatString(s).floatValue()); } public static Float valueOf(float f) { return new Float(f); } public static float parseFloat(String s) throws NumberFormatException { return FloatingDecimal.readJavaFormatString(s).floatValue(); } static public boolean isNaN(float v) { return (v != v); } static public boolean isInfinite(float v) { return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY); } private final float value; public Float(float value) { this.value = value; } public Float(double value) { this.value = (float)value; } public Float(String s) throws NumberFormatException { // REMIND: this is inefficient this(valueOf(s).floatValue()); } public boolean isNaN() { return isNaN(value); } public boolean isInfinite() { return isInfinite(value); } public String toString() { return Float.toString(value); } public byte byteValue() { return (byte)value; } public short shortValue() { return (short)value; } public int intValue() { return (int)value; } public long longValue() { return (long)value; } public float floatValue() { return value; } public double doubleValue() { return (double)value; } public int hashCode() { return floatToIntBits(value); } public boolean equals(Object obj) { return (obj instanceof Float) && (floatToIntBits(((Float)obj).value) == floatToIntBits(value)); } public static int floatToIntBits(float value) { int result = floatToRawIntBits(value); if ( ((result & FloatConsts.EXP_BIT_MASK) == FloatConsts.EXP_BIT_MASK) && (result & FloatConsts.SIGNIF_BIT_MASK) != 0) result = 0x7fc00000; return result; } public static native int floatToRawIntBits(float value); public static native float intBitsToFloat(int bits); public int compareTo(Float anotherFloat) { return Float.compare(value, anotherFloat.value); } public static int compare(float f1, float f2) { if (f1 < f2) return -1; // Neither val is NaN, thisVal is smaller if (f1 > f2) return 1; // Neither val is NaN, thisVal is larger // Cannot use floatToRawIntBits because of possibility of NaNs. int thisBits = Float.floatToIntBits(f1); int anotherBits = Float.floatToIntBits(f2); return (thisBits == anotherBits ? 0 : // Values are equal (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN) 1)); // (0.0, -0.0) or (NaN, !NaN) } /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = -2671257302660747028L; }
Double跟Float相比,就是实现了toString的一个函数,其余基本相似
package java.lang; import sun.misc.FloatingDecimal; import sun.misc.FpUtils; import sun.misc.DoubleConsts; public final class Double extends Number implements Comparable<Double> { public static final double POSITIVE_INFINITY = 1.0 / 0.0; public static final double NEGATIVE_INFINITY = -1.0 / 0.0; public static final double NaN = 0.0d / 0.0; public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308 public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308 public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324 public static final int MAX_EXPONENT = 1023; public static final int MIN_EXPONENT = -1022; public static final int SIZE = 64; public static final Class<Double> TYPE = (Class<Double>) Class.getPrimitiveClass("double"); public static String toString(double d) { return new FloatingDecimal(d).toJavaFormatString(); } public static String toHexString(double d) { /* * Modeled after the "a" conversion specifier in C99, section * 7.19.6.1; however, the output of this method is more * tightly specified. */ if (!FpUtils.isFinite(d) ) // For infinity and NaN, use the decimal output. return Double.toString(d); else { // Initialized to maximum size of output. StringBuffer answer = new StringBuffer(24); if (FpUtils.rawCopySign(1.0, d) == -1.0) // value is negative, answer.append("-"); // so append sign info answer.append("0x"); d = Math.abs(d); if(d == 0.0) { answer.append("0.0p0"); } else { boolean subnormal = (d < DoubleConsts.MIN_NORMAL); // Isolate significand bits and OR in a high-order bit // so that the string representation has a known // length. long signifBits = (Double.doubleToLongBits(d) & DoubleConsts.SIGNIF_BIT_MASK) | 0x1000000000000000L; // Subnormal values have a 0 implicit bit; normal // values have a 1 implicit bit. answer.append(subnormal ? "0." : "1."); // Isolate the low-order 13 digits of the hex // representation. If all the digits are zero, // replace with a single 0; otherwise, remove all // trailing zeros. String signif = Long.toHexString(signifBits).substring(3,16); answer.append(signif.equals("0000000000000") ? // 13 zeros "0": signif.replaceFirst("0{1,12}$", "")); // If the value is subnormal, use the E_min exponent // value for double; otherwise, extract and report d's // exponent (the representation of a subnormal uses // E_min -1). answer.append("p" + (subnormal ? DoubleConsts.MIN_EXPONENT: FpUtils.getExponent(d) )); } return answer.toString(); } } public static Double valueOf(String s) throws NumberFormatException { return new Double(FloatingDecimal.readJavaFormatString(s).doubleValue()); } public static Double valueOf(double d) { return new Double(d); } public static double parseDouble(String s) throws NumberFormatException { return FloatingDecimal.readJavaFormatString(s).doubleValue(); } static public boolean isNaN(double v) { return (v != v); } static public boolean isInfinite(double v) { return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY); } private final double value; public Double(double value) { this.value = value; } public Double(String s) throws NumberFormatException { // REMIND: this is inefficient this(valueOf(s).doubleValue()); } public boolean isNaN() { return isNaN(value); } public boolean isInfinite() { return isInfinite(value); } public String toString() { return toString(value); } public byte byteValue() { return (byte)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 int hashCode() { long bits = doubleToLongBits(value); return (int)(bits ^ (bits >>> 32)); } public boolean equals(Object obj) { return (obj instanceof Double) && (doubleToLongBits(((Double)obj).value) == doubleToLongBits(value)); } public static long doubleToLongBits(double value) { long result = doubleToRawLongBits(value); // Check for NaN based on values of bit fields, maximum // exponent and nonzero significand. if ( ((result & DoubleConsts.EXP_BIT_MASK) == DoubleConsts.EXP_BIT_MASK) && (result & DoubleConsts.SIGNIF_BIT_MASK) != 0L) result = 0x7ff8000000000000L; return result; } public static native long doubleToRawLongBits(double value); public static native double longBitsToDouble(long bits); public int compareTo(Double anotherDouble) { return Double.compare(value, anotherDouble.value); } public static int compare(double d1, double d2) { if (d1 < d2) return -1; // Neither val is NaN, thisVal is smaller if (d1 > d2) return 1; // Neither val is NaN, thisVal is larger // Cannot use doubleToRawLongBits because of possibility of NaNs. long thisBits = Double.doubleToLongBits(d1); long anotherBits = Double.doubleToLongBits(d2); return (thisBits == anotherBits ? 0 : // Values are equal (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN) 1)); // (0.0, -0.0) or (NaN, !NaN) } /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = -9172774392245257468L; }