1/2 is 0
Small integers can be represented, and integer division produces a BigInteger
result of 0.
int
BigInteger
objects have the same operations as ordinary int
s.
It is as if you had int
s represented with as many bits as needed.
For the most part, any arithmetic you can do with int
s you can do with BigInteger
s
and the operation will work the same way.
Plus, BigInteger
s allow some additional operations.
Notes:
1. BigInteger
is a class (not a primitive type). BigIntegers are objects.
2. import java.math.BigInteger
to use the class.
3. The BigInteger
objects are immutable. The value of a BigInteger
object does not change,
although a particular BigInteger
reference variable can point to different objects at different times.
4. Arithmetic operations (and other operations) are implemented as methods of the class.
5. The usual operators (+, -, *, /
) will not work. Use methods add, subtract, multiply, divide.
6. The result of using add, subtract, multiply, divide
methods is another BigInteger
.
7. The constructor takes a String
of decimal digits representing an integer of any size.
The String
is converted into the internal representation used by BigInteger
.
8. The toString()
method of BigInteger
is automatically invoked when a String
is needed (as in the last statement above.)
9. BigInteger
documentation at Oracle:
BigInteger
What do you imagine is the output of the following?
BigInteger a = new BigInteger( "70000000000" ); BigInteger b = new BigInteger( "20000000000" ); System.out.println( a.divide( b ) );
In the last statement, a.divide( b )
produces an new BigInteger
.
The println
automatically calls the toString
of that object to create a String
which is then printed.
Neither a
nor b
changes.