Java – How to convert int to BigInteger?

In Java, we can use BigInteger.valueOf(int) to convert int to a BigInteger

JavaExample.java

package com.mkyong;

import java.math.BigInteger;

public class JavaExample {

    public static void main(String[] args) {

        int n = 100;
        System.out.println(n);

        // convert int to Integer
        Integer integer = Integer.valueOf(n);
        System.out.println(integer);

        // convert int to BigInteger
        BigInteger bigInteger = BigInteger.valueOf(n);
        System.out.println(bigInteger);

        // convert Integer to BigInteger
        //BigInteger bigInteger2 = BigInteger.valueOf(integer); // works
        BigInteger bigInteger2 = BigInteger.valueOf(integer.intValue());
        System.out.println(bigInteger2);

    }

}

Output


100
100
100
100

References

  1. BigInteger JavaDoc
author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
newest oldest most voted
Mohamed Sahil
Guest
Mohamed Sahil

int n = 100;
BigInteger bigInteger = BigInteger.valueOf(n);

when we write like this it is throwing error.
BigInteger.valueOf() only accepting long value not int value.
So we have to store value like:

long n = 100;
BigInteger bigInteger = BigInteger.valueOf(n);