Java 8 | BigInteger longValueExact() Method with Examples
Last Updated :
04 Dec, 2018
java.math.BigInteger.longValueExact() was introduced in Java 8. It is an inbuilt function which converts the value of BigInteger to a long and checks for lost information. If the value of BigInteger is greater than 9,223,372,036,854,775,807 or less than -9,223,372,036,854,775,808; the method will throw ArithmeticException as BigInteger doesn’t fit in long range.
Syntax:
public long longValueExact()
Return Value: This method returns the long value of this BigInteger.
Exception: The method throws ArithmeticException if the value of BigInteger is greater than 9,223,372,036,854,775,807 or less than -9,223,372,036,854,775,808; as this range doesn’t fit in long range.
Example:
Input: 98169894145
Output: 98169894145
Explanation: 98169894145 is given as input which is BigInteger
and long value of 98169894145 is 98169894145
Input: -65416518651
Output: -65416518651
Explanation: -65416518651 is given as input which is BigInteger
and long value of -65416518651 is -65416518651
Input: 10000000000000000000
Output: ArithmeticException
Explanation: When 10000000000000000000 is tried to convert to long,
since 10000000000000000000 > 9,223,372,036,854,775,807 (greater than a long's range),
therefore it throws an arithmetic exception.
Below programs illustrates longValueExact() method of BigInteger class:
Program 1: To demonstrate longValueExact() method for positive number <9,223,372,036,854,775,807
import java.math.*;
public class GFG {
public static void main(String[] args)
{
BigInteger big;
big = new BigInteger( "98169894145" );
System.out.println( "BigInteger value : "
+ big);
long b1 = big.longValueExact();
System.out.println( "long converted value : "
+ b1);
}
}
|
Output:
BigInteger value : 98169894145
long converted value : 98169894145
Program 2: To demonstrate longValueExact() method for negative number >-9,223,372,036,854,775,808
import java.math.*;
public class GFG {
public static void main(String[] args)
{
BigInteger big = new BigInteger( "-65416518651" );
System.out.println( "BigInteger value : "
+ big);
long b1 = big.longValueExact();
System.out.println( "long converted value : "
+ b1);
}
}
|
Output:
BigInteger value : -65416518651
long converted value : -65416518651
Program 3: To demonstrate longValueExact() method for negative number <-9,223,372,036,854,775,808. It will throw Arithmetic Exception
import java.math.*;
public class GFG {
public static void main(String[] args)
{
BigInteger big = new BigInteger( "-10000000000000000000" );
System.out.println( "BigInteger value : "
+ big);
try {
long b1 = big.longValueExact();
System.out.println( "long converted value : "
+ b1);
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
BigInteger value : -10000000000000000000
Exception: java.lang.ArithmeticException: BigInteger out of long range
Program 4: To demonstrate longValueExact() method for positive number >9,223,372,036,854,775,807. It will throw Arithmetic Exception
import java.math.*;
public class GFG {
public static void main(String[] args)
{
BigInteger big = new BigInteger( "10000000000000000000" );
System.out.println( "BigInteger value : "
+ big);
try {
long b1 = big.longValueExact();
System.out.println( "long converted value : "
+ b1);
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
BigInteger value : 10000000000000000000
Exception: java.lang.ArithmeticException: BigInteger out of long range
Reference: https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#longValueExact–
Similar Reads
AtomicInteger longValue() method in Java with examples
The java.util.concurrent.atomic.AtomicInteger.longValue() is an inbuilt method in java that returns the value which is currently stored in the object by converting it into a long data-type which is a primitive conversion. Syntax: public int longValue() Parameters: The function does not accepts a sin
1 min read
BigInteger intValueExact() Method in Java with Examples
java.math.BigInteger.intValueExact() was introduced in Java 8. It is an inbuilt function which converts the value of BigInteger to a int and checks for lost information. If the value of BigInteger is greater than 2,147,483,647 or less than -2,147,483,648; the method will throw ArithmeticException as
3 min read
Byte longValue() method in Java with examples
The longValue() method of Byte class is a built in method in Java which is used to return the value of this Byte object as long. Syntax ByteObject.longValue() Return Value: It return the value of ByteObject as long. Below is the implementation of longValue() method in Java: Example 1: // Java code t
2 min read
Java 8 | BigInteger shortValueExact() Method with Examples
java.math.BigInteger.shortValueExact() was introduced in Java 8. It is an inbuilt function which converts the value of BigInteger to a short and checks for lost information. If the value of BigInteger is greater than 32,767 or less than -32,768; the method will throw ArithmeticException as BigIntege
3 min read
DoubleAdder longValue() method in Java with Examples
The java.DoubleAdder.longValue() is an inbuilt method in java that returns the sum() as a long after a narrowing primitive conversion. When the object of the class is created its initial value is zero. Syntax: public long longValue() Parameters:Return value: This method returns the numeric value rep
1 min read
BigInteger add() Method in Java with Examples
The java.math.BigInteger.add(BigInteger val) is used to calculate the Arithmetic sum of two BigIntegers. This method is used to find arithmetic addition of large numbers of range much greater than the range of biggest data type double of java without compromising with the precision of the result. Th
3 min read
AtomicLong longValue() method in Java with examples
The Java.util.concurrent.atomic.AtomicLong.longValue() is an inbuilt method in java that returns the value which is currently stored in the object and its data-type is long. Syntax: public long longValue() Parameters: The function does not accepts a single parameter. Return value: The function retur
1 min read
BigInteger longValue() Method in Java
The java.math.BigInteger.longValue() converts this BigInteger to a long value. If the value return by this function is too big to fit into long value, then it will return only the low-order 64 bits. There is chance that this conversion can lose information about the overall magnitude of the BigInteg
2 min read
BigInteger gcd() Method in Java with Examples
GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them. The java.math.BigInteger.gcd(BigInteger val) method is used to calculate gcd of two BigIntegers. This method calculates gcd upon the current BigInteger by which this method is
2 min read
BigInteger divide() Method in Java with Examples
The java.math.BigInteger.divide(BigInteger val) is used to calculate the division of two BigIntegers. BigInteger class internally uses array of integers for processing, the operation on object of BigIntegers are not as fast as on primitives. This method performs an operation upon the current BigInte
3 min read