Java基础学习之Double学习
构造方法
public Double(double value) {
this.value = value;
}
public Double(String s) throws NumberFormatException {
value = parseDouble(s);
}
成员变量
//无穷大的常数 ,只有浮点数才有无穷的概念,整型是没有的,比如1/0 得到异常
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 int BYTES = SIZE / Byte.SIZE;
常用方法
toString
调用FloatingDecimal的toJavaFormatString方法
关于FloatingDecimal的知识将在后续中进一步学习
public static String toString(double d) {
return FloatingDecimal.toJavaFormatString(d);
}
//转为16进制字符串
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 (!isFinite(d) )
// For infinity and NaN, use the decimal output.
return Double.toString(d);
else {
// Initialized to maximum size of output.
StringBuilder answer = new StringBuilder(24);
if (Math.copySign(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}$", ""));
answer.append('p');
// 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(subnormal ?
DoubleConsts.MIN_EXPONENT:
Math.getExponent(d));
}
return answer.toString();
}
}
valueOf
装箱
public static Double valueOf(String s) throws NumberFormatException {
return new Double(parseDouble(s));
}
public static Double valueOf(double d) {
return new Double(d);
}
parseDouble
调用FoatingDecimal的parseDouble方法
public static double parseDouble(String s) throws NumberFormatException {
return FloatingDecimal.parseDouble(s);
}
isNaN
这里要特别注意的是不能检测一个特定值是否等于 Double.NaN,比如 if(x=Double.NaN) 这里永远都会返回false,
所有“非数值”的值都认为是不相同的。然而,可以使用Double.isNaN方法:if(Double.isNaN(x))
public boolean isNaN() {
return isNaN(value);
}
public static boolean isNaN(double v) {
return (v != v);
}
hashCode
public int hashCode() {
return Double.hashCode(value);
}
public static int hashCode(double value) {
long bits = doubleToLongBits(value);
return (int)(bits ^ (bits >>> 32));
}
equals
先装箱将对象转为Double,再拆箱转为double,最后调用doubleToLongBits方法转为long 来比较两个对象是否相等,
如果是Float类型,此处过程类似,不同的是最后调用的是floatToIntBits转为int来比较
public boolean equals(Object obj) {
return (obj instanceof Double)
&& (doubleToLongBits(((Double)obj).value) ==
doubleToLongBits(value));
}
//double转 long ,调用的doubleToRawLongBits方法
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);
compareTo
先拆箱,再进行比较大小
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
//如果两个double相等,则继续将double转为long后比较
// 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)
}