当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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";


相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Java lang.Long.byteValue() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。