When you calculate factorial of a relatively higher number most of the data type in Java goes out of their limit. For example, you cannot use int or long variable to store factorial of a number greater than 50. In those scenarios where int and long are not big enough to represent an integral value, you can use java.math.BigInteger class. BigInteger variable can represent any integral number, there is no theoretical limit, but it allocates only enough memory required to hold all the bits of data it is asked to hold. There is not many time you need this class but its good to know about it and that's why I am giving one scenario which is perfectly suitable for using BigInteger class. In this article, you will learn how to calculate factorial of large number using BigInteger object. We will use same formula, we have used to calculate normal factorials as discussed in my previous post about factorials.

##

Here is our sample Java program to calculate large factorials. You will use BigInteger class to hold the result of calculation. Remember this class is not inside java.lang package, hence you need to explicitly import it, as we have done in this example. Another thing to keep in mind is that, this class is Immutable and any change on BigInteger object will result in another object. So, if you forget to store the result back, you will lose it. Original object will always remain in same state even after addition, subtraction or multiplication.

There is no maximum and minimum limit of BigInteger class, in theory there is no limit for BigInteger. You should use BigInteger whenever you need to represent a big integer number, i.e. something which is not floating point but cannot be represented using long data type.

1. BigInteger class is in java.math package, so you need to specifically import it, it will not be imported automatically like java.lang package.

2. Similar to String, BigInteger class is also immutable, so any modification e.g. addition, multiplication will produce a new instance of BigInteger, leaving original object intact.

3. Theoretically BigInteger has no limit of numbers.

4. BigInteger class helps to deal with very large numbers in Java.

hope this helps.

Complete Java Masterclass

Java Fundamentals: The Java Language

Java In-Depth: Become a Complete Java Engineer!

##
__How to use BigInteger class in Java__

Here is our sample Java program to calculate large factorials. You will use BigInteger class to hold the result of calculation. Remember this class is not inside java.lang package, hence you need to explicitly import it, as we have done in this example. Another thing to keep in mind is that, this class is Immutable and any change on BigInteger object will result in another object. So, if you forget to store the result back, you will lose it. Original object will always remain in same state even after addition, subtraction or multiplication.import java.math.BigInteger; /** * Java Program to calculate factorial of large numbers using BigInteger class. * * @author WINDOWS 8 */ public class BigIntegerDemo { public static void main(String args[]) { BigInteger result = factorial(BigInteger.valueOf(5)); System.out.println("factorial of 5 : " + result); result = factorial(BigInteger.valueOf(10)); System.out.println("factorial of 10 : " + result); result = factorial(BigInteger.valueOf(50)); System.out.println("factorial of 50 : " + result); } /** * Java method to calculate factorial of large number. * * @param number BigInteger * @return factorial of number as BigInteger */ public static BigInteger factorial(BigInteger number) { // factorial of 0 and 1 is always 1 - base case if (number == BigInteger.ZERO || number == BigInteger.ONE) { return BigInteger.ONE; } // !n = n*!n-1 - recursive call return number.multiply(factorial(number.subtract(BigInteger.ONE))); } } Output : factorial of 5 : 120 factorial of 10 : 3628800 factorial of 50 : 30414093201713378043612608166064768844377641568960512000000000000

There is no maximum and minimum limit of BigInteger class, in theory there is no limit for BigInteger. You should use BigInteger whenever you need to represent a big integer number, i.e. something which is not floating point but cannot be represented using long data type.

__Things to Remember :__1. BigInteger class is in java.math package, so you need to specifically import it, it will not be imported automatically like java.lang package.

2. Similar to String, BigInteger class is also immutable, so any modification e.g. addition, multiplication will produce a new instance of BigInteger, leaving original object intact.

3. Theoretically BigInteger has no limit of numbers.

4. BigInteger class helps to deal with very large numbers in Java.

hope this helps.

**Further Learning**Complete Java Masterclass

Java Fundamentals: The Java Language

Java In-Depth: Become a Complete Java Engineer!

## No comments:

## Post a Comment