Double类是原始类型double的包装类,它包含若干有效处理double值的方法,如将其转换为字符串表示形式,反之亦然。Double类的对象可以包含一个double值。
Double
类包装原始类型的值 double
中的对象。类型的对象 Double
包含一个类型为的字段 double
。
此外,这个类提供了转换的几种方法 double
到String
和 String
一个double
带有打交道时,以及其他常量和方法有用 double
。
public final class Double extends Number implements Comparable<Double> {}
//一个保持正无穷大的 double 类型常数
public static final double POSITIVE_INFINITY = 1.0 / 0.0;
//一个保持负无穷大的 double 类型常数
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
//值为NaN(Not a Number,非数)的一个 double 类型常数
public static final double NaN = 0.0d / 0.0;
//一个double类型常量存储double的有限最大值
public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308
//一个double类型常量存储double的有限的最小正数
public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308
//保持最小双精度类型的最小非零的常数
public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324
//double变量可以拥有的最大指数值。
public static final int MAX_EXPONENT = 1023;
//double变量可以拥有的最小指数值。
public static final int MIN_EXPONENT = -1022;
//一个double类型变量为64位,即8个字节。
public static final int SIZE = 64;
//用于表示双精度值(double值)的字节数
public static final int BYTES = SIZE / Byte.SIZE;
//该类的实例表示基本类型double。
@SuppressWarnings("unchecked")
public static final Class<Double> TYPE = (Class<Double>) Class.getPrimitiveClass("double");
//构造一个新分配的Double对象,该对象表示原始double参数。
public Double(double value) {
this.value = value;
}
//构造一个新分配的Double对象,该对象表示double 字符串表示的类型的浮点值。
public Double(String s) throws NumberFormatException {
value = parseDouble(s);
}
返回对应于double值的字符串。
public String toString(double d){
return FloatingDecimal.toJavaFormatString(d);
}
public String toString() {
return toString(value);
}
返回使用提供的值初始化的Double对象。
public static Double valueOf(String s) throws NumberFormatException {
return new Double(parseDouble(s));
}
public static Double valueOf(double d) {
return new Double(d);
}
通过解析字符串返回double值。与valueOf()不同,因为它返回一个原始double值,valueOf()返回Double对象。
public static double parseDouble(String s) throws NumberFormatException {
return FloatingDecimal.parseDouble(s);
}
public byte byteValue(){ return (byte)value;}
返回与此双重对象相对应的短值。
public short shortValue(){ return (short)value;}
返回与此双重对象相对应的int值。
public int intValue(){ return (int)value;}
返回与此双重对象相对应的长整型值。
public long longValue(){ return (long)value;}
返回与此双重对象相对应的double值。
public double doubleValue(){ return value;}
返回与此双重对象相对应的浮点值。
public float floatValue() {
return (float)value;
}
返回对应于这个Double对象的哈希码。
public int hashCode() {
return Double.hashCode(value);
}
如果所考虑的双对象不是数字,则返回true,否则返回false。
public boolean isNaN() {
return isNaN(value);
}
如果我们不需要创建任何双重对象,则可以使用另一种静态方法是NaN(double val)。它提供了与上述版本类似的功能。
public static boolean isNaN(double val) {
return (v != v);
}
返回参数double值的十六进制表示形式。
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();
}
}
用于比较两个Double对象的相等性。如果两个对象都包含相同的double值,则此方法返回true。只有在检查平等的情况下才能使用。在其他所有情况下,compareTo方法应该是首选。
public boolean equals(Object obj) {
return (obj instanceof Double)
&& (doubleToLongBits(((Double)obj).value) ==
doubleToLongBits(value));
}
用于比较两个Double对象的数值相等性。这应该用于比较两个Double值的数值相等性,因为它会区分较小值和较大值。返回小于0,0的值,大于0的值小于,等于和大于。
public int compareTo(Double anotherDouble) {
return Double.compare(value, anotherDouble.value);
}
用于比较两个原始double值的数值相等。因为它是一个静态方法,因此可以在不创建任何Double对象的情况下使用它。
public int compareTo(Double anotherDouble) {
return Double.compare(value, anotherDouble.value);
}
由于不同机器所选用的基数、尾数位长度和阶码位长度不同,因此对浮点数的表示有较大差别,这不利于软件在不同计算机之间的移植。为此,美国IEEE(电器及电子工程师协会)提出了一个从系统角度支持浮点数的表示方法,称为IEEE754标准(IEEE,1985),当今流行的计算机几乎都采用了这一标准。