Create BigInteger via string in Java

Java 8Object Oriented ProgrammingProgramming

BigInteger class is used for big integer calculations which are outside the limit of the primitive data types. It provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.

Firstly, set a string −

String str = "268787878787687";

Now, create a new object for BigInteger and pass the above string −

BigInteger bInteger = new BigInteger(str);

The following is an example −

Example

 Live Demo

import java.math.BigInteger;
public class Demo {
   public static void main(String[] argv) throws Exception {
      String str = "268787878787687";
      BigInteger bInteger = new BigInteger(str);
      System.out.println(bInteger);
   }
}

Output

268787878787687
raja
Published on 20-Dec-2018 05:51:52
Advertisements