We can find the maximum length of Integer with Built-in MAX_VALUE. But for BigInteger it doesn't work.
When I try to use MAX_VALUE for BigInteger it gives >> ERROR >> can not find symbol variable MAX_VALUE
public class ALL_datatype_length {
public static void main(String [] args){
byte a = Byte.MAX_VALUE;
short b = Short.MAX_VALUE;
int c = Integer.MAX_VALUE;
long d = Long.MAX_VALUE;
float e = Float.MAX_VALUE;
double f = Double.MAX_VALUE;
BigInteger g = BigInteger.MAX_VALUE; // >> Shows ERROR
System.out.println(a + "\t\t\t\tMaximum value of byte");
System.out.println(b + "\t\t\t\tMaximum value of short");
System.out.println(c + "\t\t\tMaximum value of integer");
System.out.println(d + "\t\tMaximum value of long");
System.out.println(e + "\t\t\tMaximum value of float");
System.out.println(f + "\t\tMaximum value of double");
}
}
From Javadoc: Class BigInteger
BigInteger must support values in the range -2 Integer.MAX_VALUE (exclusive) to +2 Integer.MAX_VALUE (exclusive) and may support values outside of that range. An ArithmeticException is thrown when a BigInteger constructor or method would generate a value outside of the supported range. The range of probable prime values is limited and may be less than the full supported positive range of BigInteger. The range must be at least 1 to 2 500000000.
Implementation Note:
In the reference implementation, BigInteger constructors and operations throw ArithmeticException when the result is out of the supported range of -2 Integer.MAX_VALUE (exclusive) to +2 Integer.MAX_VALUE (exclusive).
Not Necessary but here is the code:
// Same can be done for the negative value as well
BigInteger twoBigInteger = BigInteger.valueOf(2);
BigInteger intMaxBigInteger = BigInteger.valueOf(Integer.MAX_VALUE);
BigInteger bigInteger = twoBigInteger.pow(intMaxBigInteger.intValue());
System.out.println(bigInteger.intValue());
Exception:
Exception in thread "main" java.lang.ArithmeticException: BigInteger would overflow supported range
at java.math.BigInteger.reportOverflow(BigInteger.java:1084)
at java.math.BigInteger.checkRange(BigInteger.java:1079)
at java.math.BigInteger.<init>(BigInteger.java:1055)
at java.math.BigInteger.shiftLeft(BigInteger.java:3174)
at java.math.BigInteger.pow(BigInteger.java:2339)
at MyClass.main(MyClass.java:7)