📜  Java Java类

📅  最后修改于: 2022-05-13 01:54:22.316000             🧑  作者: Mango

Java Java类

Double 类是原始类型 double 的包装类,它包含几种有效处理 double 值的方法,例如将其转换为字符串表示形式,反之亦然。 Double 类的对象可以保存单个双精度值。 Double 类是原始类型 double 的包装类,它包含几种有效处理 double 值的方法,例如将其转换为字符串表示形式,反之亦然。 Double 类的对象可以保存单个双精度值。

主要有两个构造函数来初始化一个 Double-object。

A. Double(double b):创建一个使用提供的值初始化的 Double-object,其中它采用一个值作为参数进行初始化。

public Double(Double d) 

参数:用于初始化的值

B. Double(String s):创建一个 Double-object,使用字符串表示提供的解析双精度值进行初始化,其中它将字节值的字符串表示形式作为参数。

public Double(String s) throws NumberFormatException

抛出异常:如果提供的字符串不代表任何双精度值,则抛出 NumberFormatException。

双类方法

MethodAction Performed
byteValue()Returns a byte value corresponding to this Double Object
compare()Compare two primitive double values for numerical equality. As it is a static method therefore it can be used without creating any object of Double.
compareTo()Used to compare two Double objects for numerical equality and returns a value less than 0,0, a value greater than 0 for less than, equal to, and greater than.
doubleValue()Returns a double value corresponding to this Double Object.
doubleToLongBits()Returns the IEEE 754 floating-point “double format” bit layout of the given double argument. 
doubleToRawLongBits()Returns the IEEE 754 floating-point “double format” bit layout of the given double argument. It differs from the previous method as it preserves the Nan values.
equals()Compare the equality of two Double objects and returns true if both the objects contain same double value.
floatValue()Returns a float value corresponding to this Double Object.
hashCode()Returns the hashcode corresponding to this Double Object.
isInfinite()Returns true if the double object in consideration is very large, otherwise false. 
isNaN()Returns true if the double object in consideration is not a number, otherwise false.
intValue()Returns an integer value corresponding to this Double Object
longValue()Returns long value corresponding to this Double Object.
longBitsToDouble()Returns double value corresponding to the long bit pattern of the argument.
parseDouble()Returns double value by parsing the string.
shortValue()Returns short value corresponding to this Double Object
toHexString()Returns hexadecimal representation of the argument double value. 
toString()Returns the string corresponding to the double value
valueOf()Returns a Double-object initialized with the value provided
valueOf(String s)Returns a Double-object initialized with the value provided

执行:

Java
// Java Program to Illustrate Double Class
// Via Demonstrating Its Methods
  
// Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Declaring and initializing
        // double and String values
        double b = 55.05;
        String bb = "45";
  
        // Construct two Double objects
        Double x = new Double(b);
        Double y = new Double(bb);
  
        // Method - toString()
        System.out.println("toString(b) = "
                           + Double.toString(b));
  
        // Method - valueOf()
        // Return Double object
        Double z = Double.valueOf(b);
        System.out.println("valueOf(b) = " + z);
        z = Double.valueOf(bb);
        System.out.println("ValueOf(bb) = " + z);
  
        // Method - parseDouble()
        // Return primitive double value
        double zz = Double.parseDouble(bb);
        System.out.println("parseDouble(bb) = " + zz);
  
        // Print statements
        System.out.println("bytevalue(x) = "
                           + x.byteValue());
        System.out.println("shortvalue(x) = "
                           + x.shortValue());
        System.out.println("intvalue(x) = " + x.intValue());
        System.out.println("longvalue(x) = "
                           + x.longValue());
        System.out.println("doublevalue(x) = "
                           + x.doubleValue());
        System.out.println("floatvalue(x) = "
                           + x.floatValue());
  
        int hash = x.hashCode();
        System.out.println("hashcode(x) = " + hash);
  
        boolean eq = x.equals(y);
        System.out.println("x.equals(y) = " + eq);
  
        int e = Double.compare(x, y);
        System.out.println("compare(x,y) = " + e);
  
        int f = x.compareTo(y);
        System.out.println("x.compareTo(y) = " + f);
  
        Double d = Double.valueOf("1010.54789654123654");
        System.out.println("isNaN(d) = " + d.isNaN());
  
        System.out.println("Double.isNaN(45.12452) = "
                           + Double.isNaN(45.12452));
  
        // Double.POSITIVE_INFINITY stores
        // the positive infinite value
        d = Double.valueOf(Double.POSITIVE_INFINITY + 1);
        System.out.println(
            "Double.isInfinite(d) = "
            + Double.isInfinite(d.doubleValue()));
  
        double dd = 10245.21452;
        System.out.println("Double.toString(dd) = "
                           + Double.toHexString(dd));
  
        long double_to_long = Double.doubleToLongBits(dd);
        System.out.println("Double.doubleToLongBits(dd) = "
                           + double_to_long);
  
        double long_to_double
            = Double.longBitsToDouble(double_to_long);
        System.out.println(
            "Double.LongBitsToDouble(double_to_long) = "
            + long_to_double);
    }
}


输出: