當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。