Home »
Java »
Java Reference »
Java BigInteger Class
Java BigInteger Class | valueOf() Method with Example
BigInteger Class valueOf() method: Here, we are going to learn about the valueOf() method of BigInteger Class with its syntax and example.
Submitted by Preeti Jain, on May 12, 2020
BigInteger Class valueOf() method
- valueOf() method is available in java.math package.
- valueOf() method is used to represent the given long value into a BigInteger.
- valueOf() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
- valueOf() method does not throw an exception at the time of representing the long value.
Syntax:
public BigInteger valueOf(long val);
Parameter(s):
- long val – represents the long value to be returned in terms of BigInteger.
Return value:
The return type of this method is BigInteger, it returns BigInteger of the given long value.
Example:
// Java program to demonstrate the example
// of BigInteger valueOf(long val) method of BigInteger
import java.math.*;
public class ValueOfOfBI {
public static void main(String args[]) {
// Instantiates a variable l_val
long l_val = 125487312456l;
// Here, this method valueOf(long) is used
// to convert the given long value into
// a BigInteger and store it in a variable
// named value_of
BigInteger value_of = BigInteger.valueOf(l_val);
System.out.println("l_val: " + l_val);
System.out.println("valueOf(long): ");
// Display value_of
System.out.println("BigInteger.valueOf(l_val): " + value_of);
}
}
Output
l_val: 125487312456
valueOf(long):
BigInteger.valueOf(l_val): 125487312456