Java initialize BigInteger value

Tags: BigInteger BigInteger valueOf BigInteger constructor

In this Java core tutorial we learn how to initialize a BigInteger object from a String or a long value using BigInteger constructor or java.math.BigInteger.valueOf() method.

How to initialize BigInteger value

In the following Java program, we show you how to use the java.math.BigInteger.valueOf() method to instantiate a BigInteger object from a given long value.

InitializeBigIntegerExample1.java

import java.math.BigInteger;

public class InitializeBigIntegerExample1 {
    public static void main(String... args) {
        long longValue = 987654321;
        BigInteger value = BigInteger.valueOf(longValue);

        System.out.println("BigInteger value: " + value);
    }
}
The output is:
BigInteger value: 987654321

In the Java program below, we show you how to create a BigInteger object using its constructor from a given String.

InitializeBigIntegerExample2.java

import java.math.BigInteger;

public class InitializeBigIntegerExample2 {
    public static void main(String... args) {
        BigInteger value = new BigInteger("1234567");

        System.out.println("BigInteger value: " + value);
    }
}
The output is:
BigInteger value: 1234567

Happy Coding 😊