{

BigInteger multiply method in Java with examples


This post shows how to multiply BigIntegers in java with examples.

You can also check my previous posts on the BigInteger class in java.

The same does not work for BigInteger objects. It gives compilation error Operator ‘*’ cannot be applied to ‘java.math.BigInteger’, ‘java.math.BigInteger’.

Here is an example using the multiplication operator

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

It provides a multiply method to do multiplication on BigInteger.

Java BigInteger multiply method with example

The multiply method returns the result of the multiplication of values.

BigInteger is immutable and the result of methods always returns a new object.

It internally uses an array of integers to do the process, The performance is less compared with Integer multiplication.

Syntax:

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

Output:

450738376
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.