[java基础原理] 数字类型原理
1.常识
2.包装类型的继承树
3.通用JAVA包装类示例
package base.com.hzeng.jdk; import java.lang.annotation.Native; public final class Integer extends Number implements Comparable<Integer> { @Native public static final int MIN_VALUE = 0x80000000; @Native public static final int MAX_VALUE = 0x7fffffff; private final int value; public Integer(int value) { this.value = value; } 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); } 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 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 int hashCode() { return Integer.hashCode(value); } public static int hashCode(int value) { //Byte,Short,Integer的hashCode是值本身 //Long: return (int)(value ^ (value >>> 32)); //Float和Double调用native方法,使用操作系统底层的实现算法 return value; } }
4.特殊的Float和Double
基本类型float和double采用科学计算法表示。
在计算机中,浮点数的存储均采用4字节的IEEE-754格式。例如,浮点数50.0的IEEE表示形式如下:二进制:
其中,最高位表示符号,"1"表示负,"0"表示正;第23~30位表示阶码。注意:阶码是以2为底的指数再加上偏移量127.第0~22位是尾数部分。尾数的整数部分永远为1,因此不予保存,但它是隐含存在的。一个浮点数计算式为:
例如,前面绘出的浮点数的表示形式中,s=0,n=132,m=(1/2+0/4+0/8+1/16+0/32+……),则计算结果为50.0.
不管是float还是double,存储的有效数字(尾数m)是有限的,所以计算机没法真实存储1/3这种无限位数的数字。因为采用近似数存储,所以浮点型数字的所有数学运算(+-*/)的结果也是近似数,精度都存在丢失问题。
现实生活中也是一样的,我们对事物的判断和计算也都是近似计算,很难得出绝对精确的结果,要求绝对完美的答案和结果本身就是问题。找对象也一样,没有最好的,只有近似合适的,两个人在一起相处愉快就好,追求高富帅,白富美之前,请先确定自己是否完美。
参考: