📌  相关文章
📜  Java中的Java lang.Long.byteValue() 方法和示例

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

Java中的Java lang.Long.byteValue() 方法和示例


Java .lang.Long.byteValue()是Java中的一个内置函数,它以字节的形式返回这个Long 的值。

句法:

public byte byteValue()
Parameters: The function does not accept any parameter.
Return :
This method returns the numeric value 
represented by this object after conversion to byte type.

例子:

Input : 12
Output : 12

Input : 1023
Output : -1

下面的程序说明了Java.lang.Long.byteValue()函数:

程序 1:

// Java program that demonstrates the use of
// Long.byteValue() function
  
// include lang package
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        Long value = 1023l;
  
        // returns the value of Long as a byte
        byte byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
  
        // 2nd example
        value = 12l;
        byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
    }
}

输出:

Byte Value of num = -1
Byte Value of num = 12

程序2:演示负数的字节值

// Java program that demonstrates the use of
// Long.byteValue() function
// negative number
  
// include lang package
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        Long value = -1023l;
  
        // returns the value of Long as a byte
        byte byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
  
        // 2nd example
        value = -12l;
        byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
    }
}

输出:

Byte Value of num = 1
Byte Value of num = -12

程序 3:在参数中传递十进制值时。

// Java program that demonstrates the use of
// Long.byteValue() function
// decimal number
  
// include lang package
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        Long value = 11.24;
  
        // returns the value of Long as a byte
        byte byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
    }
}

输出:

prog.java:13: error: incompatible types: double cannot be converted to Long
        Long value = 11.24;

程序 4:在参数中传递字符串值时。

// Java program that demonstrates the use of
// Long.byteValue() function
// string number
  
// include lang package
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        Long value = "24";
  
        // returns the value of Long as a byte
        byte byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
    }
}

输出:

prog.java:13: error: incompatible types: String cannot be converted to Long
        Long value = "24";