Java - Long byteValue() 方法



描述

Java Long byteValue() 方法将此 Long 的值返回为 byte 类型。

声明

以下是 java.lang.Long.byteValue() 方法的声明

public byte byteValue()

参数

返回值

此方法在转换为 byte 类型后返回此对象表示的数值。

异常

从具有正值的 Long 对象获取 byte 值示例

以下示例演示了如何使用 Long byteValue() 方法从 long 获取 byte。我们创建了一个 Long 变量,并为其分配了一个使用正 long 值创建的 Long 对象。然后使用 byteValue() 方法,我们打印来自 Long 对象的 byte。

package com.tutorialspoint;
public class LongDemo {
   public static void main(String[] args) {
      Long obj = new Long(10L);
 
      // returns the value of Long as a byte
      byte b = obj.byteValue();
      System.out.println("Value of b = " + b);
   }
} 

输出

让我们编译并运行上述程序,这将产生以下结果:

Value of b = 10

从具有负值的 Long 对象获取 byte 值示例

以下示例演示了如何使用 Long byteValue() 方法从 long 获取 byte。我们创建了一个 Long 变量,并为其分配了一个使用负 long 值创建的 Long 对象。然后使用 byteValue() 方法,我们打印来自 Long 对象的 byte。

package com.tutorialspoint;
public class LongDemo {
   public static void main(String[] args) {
      Long obj = new Long(-10L);
 
      // returns the value of Long as a byte
      byte b = obj.byteValue();
      System.out.println("Value of b = " + b);
   }
} 

输出

让我们编译并运行上述程序,这将产生以下结果:

Value of b = -10

从具有零值的 Long 对象获取 byte 值示例

以下示例演示了如何使用 Long byteValue() 方法从 long 获取 byte。我们创建了一个 Long 变量,并为其分配了一个使用零 long 值创建的 Long 对象。然后使用 byteValue() 方法,我们打印来自 Long 对象的 byte。

package com.tutorialspoint;
public class LongDemo {
   public static void main(String[] args) {
      Long obj = new Long(0L);
 
      // returns the value of Long as a byte
      byte b = obj.byteValue();
      System.out.println("Value of b = " + b);
   }
} 

输出

让我们编译并运行上述程序,这将产生以下结果:

Value of b = 0

从具有负零值的 Long 对象获取 byte 值示例

以下示例演示了如何使用 Long byteValue() 方法从 long 获取 byte。我们创建了一个 Long 变量,并为其分配了一个使用负零 long 值创建的 Long 对象。然后使用 byteValue() 方法,我们打印来自 Long 对象的 byte。

package com.tutorialspoint;
public class LongDemo {
   public static void main(String[] args) {
      Long obj = new Long(-0L);
 
      // returns the value of Long as a byte
      byte b = obj.byteValue();
      System.out.println("Value of b = " + b);
   }
} 

输出

让我们编译并运行上述程序,这将产生以下结果:

Value of b = 0
java_lang_long.htm
广告

© . All rights reserved.