BigInteger byteValueExact() in Java

byteValueExact(): This method is available in java.math.BigInteger class of Java.

Syntax:

byte java.math.BigInteger.byteValueExact()

It converts this BigInteger to a byte, checking for lost information. If the value of this BigIntegeris out of the range of the byte type, then an ArithmeticException is thrown.

Returns: this BigInteger is converted to a byte.

Throws: ArithmeticException - if the value of this will not exactly fit in a byte.

For Example:

BigInteger bigInteger = new BigInteger("12")

bigInteger.byteValueExact() = > It returns 12.

Approach

Java

import java.math.BigInteger;

public class BigIntegerbyteValueExact {
    public static void main(String[] args) {

        BigInteger bigInteger = new BigInteger("12");
        System.out.println(bigInteger.byteValueExact());
    }
}

Output:

12

No comments:

Post a Comment