📜  Java中的 Double shortValue() 方法及示例

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

Java中的 Double shortValue() 方法及示例

Double 类的shortValue()方法是一个内置方法,用于在类型转换后将调用对象指定的值返回为 short。
语法

DoubleObject.shortValue()

返回值:它将 DoubleObject 的值返回为short
下面的程序说明了Java中的 shortValue() 方法:
方案一:

Java
// Java code to demonstrate
// Double shortValue() method
 
class GFG {
    public static void main(String[] args)
    {
 
        // Double value
        Double a = 17.47;
 
        // wrapping the Double value
        // in the wrapper class Double
        Double b = new Double(a);
 
        // shortValue of the Double Object
        short output = b.shortValue();
 
        // print shorting the output
        System.out.println("Short value of "
                           + b + " is : " + output);
    }
}


Java
// Java code to demonstrate
// Double shortValue() method
 
class GFG {
    public static void main(String[] args)
    {
 
        String value = "54.1";
 
        // wrapping the Double value
        // in the wrapper class Double
        Double b = new Double(value);
 
        // shortValue of the Double Object
        short output = b.shortValue();
 
        // print shorting the output
        System.out.println("Short value of "
                           + b + " is : " + output);
    }
}


输出:
Short value of 17.47 is : 17

方案二:

Java

// Java code to demonstrate
// Double shortValue() method
 
class GFG {
    public static void main(String[] args)
    {
 
        String value = "54.1";
 
        // wrapping the Double value
        // in the wrapper class Double
        Double b = new Double(value);
 
        // shortValue of the Double Object
        short output = b.shortValue();
 
        // print shorting the output
        System.out.println("Short value of "
                           + b + " is : " + output);
    }
}
输出:
Short value of 54.1 is : 54