Fix for java.math.BigInteger cannot be cast to java.lang.Integer?

Java 8Object Oriented ProgrammingProgramming

You can typecast with the help of method intValue(). The syntax is as follows −

Integer yourVariableName=((BigInteger) yourBigIntegerValue).intValue();

Here is the Java code to convert java.math.BigInteger cast to java.lang.Integer. The code is as follows −


 Live Demo

import java.math.BigInteger;
public class BigIntegerToInteger {
   public static void main(String []args) {
      BigInteger bigValue [] = new BigInteger[5];
      bigValue[0] = new BigInteger("6464764");
      bigValue[1] = new BigInteger("212112221122");
      bigValue[2] = new BigInteger("76475");
      bigValue[3] = new BigInteger("94874747");
      bigValue[4] = new BigInteger("2635474");
      for(int i = 0; i< bigValue.length; i++) {
         Integer value = ((BigInteger) bigValue[i]).intValue();
         System.out.println("Integer value is:"+value);

Here is the sample output −


Integer value is:6464764
Integer value is:1658823618
Integer value is:76475
Integer value is:94874747
Integer value is:2635474
Updated on 30-Jul-2019 22:30:25