Java 简明教程

Java - Double class

Introduction

Java Double 类将原始类型 double 的值包装在对象中。Double 类型对象包含一个字段,其类型为 double。

The Java Double class wraps a value of primitive type double in an object. An object of type Double contains a single field whose type is double.

Class Declaration

以下是 java.lang.Double 类的声明 −

Following is the declaration for java.lang.Double class −

public final class Double
   extends Number
      implements Comparable<Double>

Field

以下是 java.lang.Double 类的字段 −

Following are the fields for java.lang.Double class −

  1. static int MAX_EXPONENT − This is the maximum exponent a finite double variable may have.

  2. static double MAX_VALUE − This is the constant holding the largest positive finite value of type double, (2-2-52)×21023.

  3. static int MIN_EXPONENT − This is the minimum exponent a normalized double variable may have.

  4. static double MIN_NORMAL − This is the constant holding the smallest positive normal value of type double, 2-1022.

  5. static double MIN_VALUE − This is the constant holding the smallest positive nonzero value of type double, 2-1074.

  6. static double NaN − This is the constant holding a Not-a-Number (NaN) value of type double.

  7. static double NEGATIVE_INFINITY − This is the constant holding the negative infinity of type double.

  8. static double POSITIVE_INFINITY − This is the constant holding the positive infinity of type double.

  9. static int SIZE − This is the number of bits used to represent a double value.

  10. static Class<Double> TYPE − This is the class instance representing the primitive type double

Class constructors

Sr.No.

Constructor & Description

1

Double(double value) This constructs a newly allocated Double object that represents the primitive double argument.

2

Double(String s) This constructs a newly allocated Double object that represents the floating-point value of type double represented by the string.

Class methods

Sr.No.

Method & Description

1

byte byteValue()This method returns the value of this Double as a byte (by casting to a byte).

2

static int compare(double d1, double d2) This method compares the two specified double values.

3

int compareTo(Double anotherDouble) This method compares the two specified double values.

4

static long doubleToLongBits(double value)This method returns a representation of the specified floating-point value according to the IEEE 754 floating-point "double format" bit layout.

5

static long doubleToRawLongBits(double value)This method returns a representation of the specified floating-point value according to the IEEE 754 floating-point "double format" bit layout, preserving Not-a-Number (NaN) values.

6

double doubleValue()This method returns a representation of the specified floating-point value according to the IEEE 754 floating-point "double format" bit layout, preserving Not-a-Number (NaN) values.

7

boolean equals(Object obj)This method compares this object against the specified object.

8

float floatValue()This method returns the float value of this Double object.

9

int hashCode()This method returns a hash code for this Double object.

10

int intValue()This method returns the value of this Double as an int (by casting to type int).

11

boolean isInfinite()This method returns true if this Double value is infinitely large in magnitude, false otherwise.

12

static boolean isInfinite(double v)This method returns true if the specified number is infinitely large in magnitude, false otherwise.

13

boolean isNaN()This method returns true if this Double value is a Not-a-Number (NaN), false otherwise.

14

static boolean isNaN(double v)This method returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.

15

static double longBitsToDouble(long bits)This method returns the double value corresponding to a given bit representation.

16

long longValue()This method returns the value of this Double as a long (by casting to type long).

17

static double parseDouble(String s)This method returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.

18

short shortValue()This method returns the value of this Double as a short (by casting to a short).

19

static String toHexString(double d)This method returns a hexadecimal string representation of the double argument.

20

String toString()This method returns a string representation of this Double object.

21

static String toString(double d) This method returns a string representation of the double argument.

22

static Double valueOf(double d)This method returns a Double instance representing the specified double value.

23

static Double valueOf(String s)This method returns a Double object holding the double value represented by the argument string s.

Methods inherited

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. java.lang.Object

Example

以下示例展示了如何使用 Double 类从字符串中获取双精度值。我们已经创建了一个字符串,然后使用 valueOf() 方法检索获取一个 Double 对象,然后打印双精度对象。

The following example shows the usage of Double class to get double from a string. We’ve created a String and then using valueOf() method, we’re retrieving getting a Double Object and then double object is printed.

package com.tutorialspoint;

public class DoubleDemo {
   public static void main(String[] args) {

      // create a String s and assign value to it
      String s = "+120";

      // create a Double object b
      Double b;

      // get the value of double from string
      b = Double.valueOf(s);

      // print b value
      System.out.println( "Double value of string " + s + " is " + b );
   }
}

Output

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

Double value of string +120 is 120.0