📜  Java 8 |带有示例的 BigInteger byteValueExact() 方法

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

Java 8 |带有示例的 BigInteger byteValueExact() 方法

Java.math.BigInteger.byteValueExact()是在Java 8 中引入的。它是一个内置函数,可将 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:演示正数 <127 的 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;
        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:演示负数 >-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("-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:演示正数 >127 的 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

参考: https: Java/math/BigInteger.html#byteValueExact–