Java Float shortValue()方法及示例

来源:这里教程网 时间:2026-02-17 20:55:20 作者:

Java Float shortValue()方法及示例

Float类中shortValue() 方法是Java中的一个内置方法,它可以在类型转换后返回将对象调用为short的指定值。

语法

public short shortValue()

返回值: 它将FloatObject的值作为 短值 返回 。

以下程序说明了Java中的 shortValue() 方法:

程序1 :

// Java code to demonstrate// Float shortValue() method class GFG {    public static void main(String[] args)    {         // Float value        Float a = 17.47f;         // wrapping the Float value        // in the wrapper class Float        Float b = new Float(a);         // shortValue of the Float 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

程序2

// Java code to demonstrate// Float shortValue() method class GFG {    public static void main(String[] args)    {         String value = "54.1f";         // wrapping the Float value        // in the wrapper class Float        Float b = new Float(value);         // shortValue of the Float 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

参考资料: https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#shortValue()

相关推荐