This one is Zoho Interview Question. The interview question asked to a fresher Java candidate from Payilagam in Zoho is – How to calculate YouTube views when it crosses long size also?
The answer is very simple. There is a class called BigInteger in Java. If we have any mathematical calculation that crosses the limit of all available Primitive Data types in Java, we can go ahead with BigInteger.
BigInteger can contain any large Integer (which is bigger than 64 bit values). As BigInteger is a class and it stores the values in its object, memory allocated here is dynamic. (Remember, objects will have dynamic memory.). There are number of methods available in BigInteger class which you can find in https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html
Thus the answer for the asked Interview Question in Zoho – How to calculate YouTube views when it crosses long size also? is quite simple. It is, we can go ahead with BigInteger.
Power of BigInteger – Sample Program:
If we calculate the factorial value of 50, it crosses more than 50 digits for which we can go with BigInteger.
import java.math.BigInteger;
public class BigIntegerFactorial {
public static void main(String[] args) {
calculateFactorial(50);
}
public static void calculateFactorial(int n) {
BigInteger result = BigInteger.ONE;
for (int i=1; i<=n; i++) {
result = result.multiply(BigInteger.valueOf(i));
}
System.out.println(n + “! = ” + result);
}
}