Java Short byteValue()方法及示例

来源:这里教程网 时间:2026-02-17 21:09:48 作者:

Java Short byteValue()方法及示例

Short类中的 java.lang.Short.byteValue() 方法是Java中的一个内置方法,用于将Short对象的值作为一个字节返回。

语法

ShortObject.byteValue()

返回值: 它以字节形式返回ShortObject的值。

下面是byteValue()方法在Java中的实现。

例1 :

// Java code to demonstrate// Short byteValue() method  class GFG {    public static void main(String[] args)    {          // Short value        Short a = 17;          // wrapping the Short value        // in the wrapper class Short        Short b = new Short(a);          // byteValue of the Short Object        byte output = b.byteValue();          // printing the output        System.out.println("Byte value of "                           + b + " is : " + output);    }}

输出:

Byte value of 17 is : 17

例2 :

// Java code to demonstrate// Short byteValue() method  class GFG {    public static void main(String[] args)    {          String value = "17";          // wrapping the Short value        // in the wrapper class Short        Short b = new Short(value);          // byteValue of the Short Object        byte output = b.byteValue();          // printing the output        System.out.println("Byte value of "                           + b + " is : " + output);    }}

输出:

Byte value of 17 is : 17

相关推荐