JDK源码解析之 Java.lang.Double
Double類是原始類型double的包裝類,它包含若干有效處理double值的方法,如將其轉(zhuǎn)換為字符串表示形式,反之亦然。Double類的對(duì)象可以包含一個(gè)double值。
Double類包裝原始類型的值 double中的對(duì)象。類型的對(duì)象 Double包含一個(gè)類型為的字段 double。
此外,這個(gè)類提供了轉(zhuǎn)換的幾種方法 double到String和 String一個(gè)double帶有打交道時(shí),以及其他常量和方法有用 double。
一、類定義
public final class Double extends Number implements Comparable<Double> {}- 類被聲明為final的,表示不能被繼承;
- 繼承了Number抽象類,可以用于數(shù)字類型的一系列轉(zhuǎn)換;
- 實(shí)現(xiàn)了Comparable接口,強(qiáng)行對(duì)實(shí)現(xiàn)它的每個(gè)類的對(duì)象進(jìn)行整體排序
二、成員變量
//一個(gè)保持正無(wú)窮大的 double 類型常數(shù)public static final double POSITIVE_INFINITY = 1.0 / 0.0;//一個(gè)保持負(fù)無(wú)窮大的 double 類型常數(shù)public static final double NEGATIVE_INFINITY = -1.0 / 0.0;//值為NaN(Not a Number,非數(shù))的一個(gè) double 類型常數(shù)public static final double NaN = 0.0d / 0.0;//一個(gè)double類型常量存儲(chǔ)double的有限最大值public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308//一個(gè)double類型常量存儲(chǔ)double的有限的最小正數(shù)public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308//保持最小雙精度類型的最小非零的常數(shù)public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324//double變量可以擁有的最大指數(shù)值。public static final int MAX_EXPONENT = 1023;//double變量可以擁有的最小指數(shù)值。public static final int MIN_EXPONENT = -1022;//一個(gè)double類型變量為64位,即8個(gè)字節(jié)。public static final int SIZE = 64;//用于表示雙精度值(double值)的字節(jié)數(shù)public static final int BYTES = SIZE / Byte.SIZE;//該類的實(shí)例表示基本類型double。@SuppressWarnings("unchecked")public static final Class<Double> TYPE = (Class<Double>) Class.getPrimitiveClass("double");三、構(gòu)造器
//構(gòu)造一個(gè)新分配的Double對(duì)象,該對(duì)象表示原始double參數(shù)。 public Double(double value) {this.value = value;} //構(gòu)造一個(gè)新分配的Double對(duì)象,該對(duì)象表示double 字符串表示的類型的浮點(diǎn)值。 public Double(String s) throws NumberFormatException {value = parseDouble(s);}四、常用方法
toString():
返回對(duì)應(yīng)于double值的字符串。
public String toString(double d){return FloatingDecimal.toJavaFormatString(d);}public String toString() {return toString(value);}valueOf():
返回使用提供的值初始化的Double對(duì)象。
public static Double valueOf(String s) throws NumberFormatException {return new Double(parseDouble(s));}public static Double valueOf(double d) {return new Double(d);}parseDouble():
通過(guò)解析字符串返回double值。與valueOf()不同,因?yàn)樗祷匾粋€(gè)原始double值,valueOf()返回Double對(duì)象。
public static double parseDouble(String s) throws NumberFormatException {return FloatingDecimal.parseDouble(s);}byteValue():
public byte byteValue(){ return (byte)value;}shortValue():
返回與此雙重對(duì)象相對(duì)應(yīng)的短值。
public short shortValue(){ return (short)value;}intValue():
返回與此雙重對(duì)象相對(duì)應(yīng)的int值。
public int intValue(){ return (int)value;}longValue():
返回與此雙重對(duì)象相對(duì)應(yīng)的長(zhǎng)整型值。
public long longValue(){ return (long)value;}doubleValue():
返回與此雙重對(duì)象相對(duì)應(yīng)的double值。
public double doubleValue(){ return value;}floatValue():
返回與此雙重對(duì)象相對(duì)應(yīng)的浮點(diǎn)值。
public float floatValue() {return (float)value;}hashCode():
返回對(duì)應(yīng)于這個(gè)Double對(duì)象的哈希碼。
public int hashCode() {return Double.hashCode(value);}isNaN():
如果所考慮的雙對(duì)象不是數(shù)字,則返回true,否則返回false。
public boolean isNaN() {return isNaN(value);}如果我們不需要?jiǎng)?chuàng)建任何雙重對(duì)象,則可以使用另一種靜態(tài)方法是NaN(double val)。它提供了與上述版本類似的功能。
public static boolean isNaN(double val) {return (v != v);}toHexString():
返回參數(shù)double值的十六進(jìn)制表示形式。
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 infoanswer.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();}}equals():
用于比較兩個(gè)Double對(duì)象的相等性。如果兩個(gè)對(duì)象都包含相同的double值,則此方法返回true。只有在檢查平等的情況下才能使用。在其他所有情況下,compareTo方法應(yīng)該是首選。
public boolean equals(Object obj) {return (obj instanceof Double)&& (doubleToLongBits(((Double)obj).value) ==doubleToLongBits(value));}compareTo():
用于比較兩個(gè)Double對(duì)象的數(shù)值相等性。這應(yīng)該用于比較兩個(gè)Double值的數(shù)值相等性,因?yàn)樗鼤?huì)區(qū)分較小值和較大值。返回小于0,0的值,大于0的值小于,等于和大于。
public int compareTo(Double anotherDouble) {return Double.compare(value, anotherDouble.value);}compare():
用于比較兩個(gè)原始double值的數(shù)值相等。因?yàn)樗且粋€(gè)靜態(tài)方法,因此可以在不創(chuàng)建任何Double對(duì)象的情況下使用它。
public int compareTo(Double anotherDouble) {return Double.compare(value, anotherDouble.value);}五、總結(jié)
由于不同機(jī)器所選用的基數(shù)、尾數(shù)位長(zhǎng)度和階碼位長(zhǎng)度不同,因此對(duì)浮點(diǎn)數(shù)的表示有較大差別,這不利于軟件在不同計(jì)算機(jī)之間的移植。為此,美國(guó)IEEE(電器及電子工程師協(xié)會(huì))提出了一個(gè)從系統(tǒng)角度支持浮點(diǎn)數(shù)的表示方法,稱為IEEE754標(biāo)準(zhǔn)(IEEE,1985),當(dāng)今流行的計(jì)算機(jī)幾乎都采用了這一標(biāo)準(zhǔn)。
總結(jié)
以上是生活随笔為你收集整理的JDK源码解析之 Java.lang.Double的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql安装、导入数据脚本
- 下一篇: JDK源码解析之Java.util.Co