When to User BigInteger in Java

When to use Java BigInteger

Java BigInteger class is used to perform arithmetic operation or holding Numbers large enough that Integer cannot hold. In fact BigInteger does not have any limit on the numerical value that it can represent.

java.math.BigInteger

Integer Max and Min value

In Java Integer can hold between 2147483647 and -2147483648. This is denoted by Integer.MAX_VALUE and Integer.MIN_VALUE

Print Value of Integer Max and Min

Print max and min value
System.out.println(Integer.MAX_VALUE); //2147483647
System.out.println(Integer.MIN_VALUE);  //-2147483648
When we have numbers that cannot fit in the Integer range we can use BigInteger class. Also this Class provides a lot of methods to carry out different mathematical operations such as add, subtract, divide, multiply and many more
BigInteger Example
BigInteger a = BigInteger.valueOf(15);
BigInteger b = BigInteger.ONE;

BigInteger result = a.add(b);
System.out.println("Addition of a,b = " + result);

result = a.subtract(b);
System.out.println("Subtration of a,b = " + result);

result = a.divide(b);
System.out.println("Division of a,b = " + result);

result = a.multiply(b);
System.out.println("Multipllication of a,b = " + result);

No comments :

Post a Comment

Please leave your message queries or suggetions.