Java中的BigInteger乘法与实例

227 阅读1分钟

本帖通过实例展示了如何在java中进行BigIntegers的乘法。

你也可以查看我以前关于java中BigInteger 类的帖子。

同样的方法对BigInteger对象不起作用。它给出了编译错误Operator '*' 不能应用于 'java.math.BigInteger', 'java.math.BigInteger' 。

下面是一个使用乘法运算符的例子

import java.math.BigInteger;

public class BigIntegerMultiply {
    public static void main(String[] args) {
        BigInteger operand1= BigInteger.valueOf(2);
        BigInteger operand2= BigInteger.valueOf(2);
        BigInteger result=operand1*operand2;
        System.out.println(result);
    }
}

它提供了一个multiply 方法来对BigInteger进行乘法运算。

Java BigInteger乘法的例子

multiply 方法返回数值乘法的结果。

BigInteger是不可改变的,方法的结果总是返回一个新对象。

它在内部使用一个整数数组来完成这一过程,与整数乘法相比,其性能较差。

语法。

    public BigInteger multiply(BigInteger val) 
import java.math.BigInteger;

public class BigIntegerMultiply {
    public static void main(String[] args) {
        BigInteger operand1= BigInteger.valueOf(212312);
        BigInteger operand2= BigInteger.valueOf(2123);
        BigInteger result=operand1.multiply(operand2);
        System.out.println(result);
    }
}

输出。