Java 8中引入了java.math.BigInteger.byteValueExact()。它是一個內置函數,可將BigInteger的值轉換為字節,並檢查是否丟失任何信息。如果BigInteger的值大於127或小於-128,則該方法將拋出ArithmeticException,因為BigInteger不在字節範圍內。
用法:
public byte byteValueExact()
返回值:此方法返回此BigInteger的字節值。
異常:如果BigInteger的值大於127或小於-128,則此方法將引發ArithmeticException,因為此範圍不適合字節範圍。
例:
Input:122 Output:122 Explanation:122 is given as input which is bigInteger and byte value of 122 is 122 Input:-46 Output:-46 Explanation:-46 is given as input which is bigInteger and byte value of -46 is -46 Input:200 Output:ArithmeticException Explanation:When 200 is tried to convert to Byte, since 200 > 127 (greater than a Byte's range), therefore it throws an arithmetic exception.
以下示例程序旨在說明BigInteger類的byteValueExact()方法:
程序1:演示byteValueExact()方法的正數<127
// Java program to demonstrate byteValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big;
big = new BigInteger("122");
// print value of BigInteger
System.out.println("BigInteger value:"
+ big);
// convert big to the byte value
byte b1 = big.byteValueExact();
// print byte value
System.out.println("Byte converted value:"
+ b1);
}
}
輸出:
BigInteger value:122 Byte converted value:122
程序2:演示byteValueExact()方法用於負數> -128
// Java program to demonstrate byteValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big = new BigInteger("-46");
// print value of BigInteger
System.out.println("BigInteger value:"
+ big);
// convert big to the byte value
byte b1 = big.byteValueExact();
// print byte value
System.out.println("Byte converted value:"
+ b1);
}
}
輸出:
BigInteger value:-46 Byte converted value:-46
程序3:演示用於負數<-128的byteValueExact()方法。它將引發算術異常
// Java program to demonstrate byteValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big = new BigInteger("-200");
// print value of BigInteger
System.out.println("BigInteger value:"
+ big);
try {
// convert big to the byte value
byte b1 = big.byteValueExact();
// print byte value
System.out.println("Byte converted value:"
+ b1);
}
catch (Exception e) {
System.out.println("Exception:" + e);
}
}
}
輸出:
BigInteger value:-200 Exception:java.lang.ArithmeticException:BigInteger out of byte range
程序4:演示byteValueExact()方法用於正數> 127。它將引發算術異常
// Java program to demonstrate byteValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big = new BigInteger("200");
// print value of BigInteger
System.out.println("BigInteger value:"
+ big);
try {
// convert big to the byte value
byte b1 = big.byteValueExact();
// print byte value
System.out.println("Byte converted value:"
+ b1);
}
catch (Exception e) {
System.out.println("Exception:" + e);
}
}
}
輸出:
BigInteger value:200 Exception:java.lang.ArithmeticException:BigInteger out of byte range
參考: https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#byteValueExact-
相關用法
- Java BigInteger gcd()用法及代碼示例
- Java BigInteger add()用法及代碼示例
- Java BigDecimal byteValueExact()用法及代碼示例
- Java BigInteger sqrtAndRemainder()用法及代碼示例
- Java 8 BigInteger longValueExact()用法及代碼示例
- Java 8 BigInteger shortValueExact()用法及代碼示例
- Java BigInteger intValueExact()用法及代碼示例
- Java BigInteger nextProbablePrime()用法及代碼示例
- Java 8 BigInteger divideAndRemainder()用法及代碼示例
- Java BigInteger subtract()用法及代碼示例
- Java BigInteger multiply()用法及代碼示例
- Java BigInteger divide()用法及代碼示例
- Java BigInteger isProbablePrime()用法及代碼示例
- Java BigInteger xor()用法及代碼示例
- Java BigInteger and()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Java 8 | BigInteger byteValueExact() method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。