Java 8 | BigInteger byteValueExact() method with Examples

java.math.BigInteger.byteValueExact() was introduced in Java 8. It is an inbuilt function which converts the value of BigInteger to a byte and checks for any lost information. If the value of BigInteger is greater than 127 or less than -128, the method will throw ArithmeticException as BigInteger doesn’t fit in byte range.
Syntax:
public byte byteValueExact()
Return Value: This method returns the byte value of this BigInteger.
Exception: The method throws ArithmeticException if the value of BigInteger is greater than 127 or less than -128, as this range doesn’t fit in byte range.
Example:
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.
Below programs illustrates byteValueExact() method of BigInteger class:
Program 1: To demonstrate byteValueExact() method for positive number <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
Program 2: To demonstrate byteValueExact() method for negative number >-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
Program 3: To demonstrate byteValueExact() method for negative number <-128. It will throw Arithmetic Exception
// 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
Program 4: To demonstrate byteValueExact() method for positive number >127. It will throw Arithmetic Exception
// 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
Reference: https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#byteValueExact–



