📜  Java中的短 byteValue() 方法与示例

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

Java中的短 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