Java BigInteger没有最大长度吗?如果有,我如何找到BigInteger数据类型的最大长度?

Java BigInteger没有最大长度吗?如果有,我如何找到BigInteger数据类型的最大长度?,java,types,biginteger,maxlength,Java,Types,Biginteger,Maxlength,我们可以通过内置的MAX_值找到整数的最大长度。但对于BigInteger来说,它不起作用 当我尝试对BigInteger使用MAX\u值时,它给出了>>错误>>找不到符号变量MAX\u值 public class ALL_datatype_length { public static void main(String [] args){ byte a = Byte.MAX_VALUE; short b = Short.MAX_VALUE;

我们可以通过内置的MAX_值找到整数的最大长度。但对于BigInteger来说,它不起作用

当我尝试对BigInteger使用MAX\u值时,它给出了>>错误>>找不到符号变量MAX\u值

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");
    }
}

假设您有内存,并且JVM将数组限制为
Integer.MAX_VALUE-5
元素(请参阅),则
biginger
的最大值具有一个包含所有1位的
mag
数组

由于
Integer.MAX_值-5
为2147483642,且每个数组元素为32位(
int
),因此
biginger
最多可使用68719476544位

这意味着十进制数为20686623727位

这几乎是一样的无限。确实没有定义的最大值。

来自Javadoc:

BigInteger必须支持-2 Integer.MAX_值(独占)到+2 Integer.MAX_值(独占)范围内的值,并且可能支持该范围之外的值。当BigInteger构造函数或方法生成的值超出支持的范围时,将引发算术异常。可能素数值的范围是有限的,可能小于完全支持的BigInteger的正范围。范围必须至少为1到2 500000000

实施说明:

// 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());
在参考实现中,当结果超出支持的-2 Integer.MAX_值(独占)到+2 Integer.MAX_值(独占)范围时,BigInteger构造函数和操作会抛出算术异常

不需要,但代码如下:

// 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 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)

线程“main”java.lang.arithmetricException中的异常:BigInteger将溢出支持的范围 位于java.math.biginger.reportOverflow(biginger.java:1084) 位于java.math.biginger.checkRange(biginger.java:1079) 在java.math.biginger.(biginger.java:1055) 位于java.math.biginger.shiftLeft(biginger.java:3174) 位于java.math.biginger.pow(biginger.java:2339) 位于MyClass.main(MyClass.java:7)
大整数的最大值由可用内存控制,因此不存在最大值。