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 −

Example

 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 −

Output

Integer value is:6464764
Integer value is:1658823618
Integer value is:76475
Integer value is:94874747
Integer value is:2635474
raja
Published on 01-Mar-2019 06:54:11
Advertisements