3
Calculations with BigInteger
follow the same rules as ordinary integer math.
Integer division produces an integer result.
add
and subtract
BigInteger
has add, subtract, multiply, and divide.
Here is
addition and subtraction:
public BigInteger add( BigInteger val )
Addval
to theBigInteger
that contains the method. The sum is returned in a newBigInteger
public BigInteger subtract( BigInteger val )
Subtractval
from theBigInteger
that contains the method. The result is returned in a newBigInteger
BigInteger
objects are immutable.
The above methods create a new object using the values represented by the operand objects.
Here is a program:
import java.math.BigInteger; class AddSubtract { public static void main ( String[] args ) { BigInteger a = new BigInteger( "45" ); BigInteger b = new BigInteger( "12" ); BigInteger c, d; c = a.add( b ); d = c.subtract( new BigInteger( "20" ) ); System.out.println("Result: " + d ); } }
Result: 37
What do you imagine is the output of the following?
BigInteger a = new BigInteger( "10000000000" ); ; BigInteger b = new BigInteger( "00000000001" ); ; System.out.println( a.subtract( b ) );