Professional Documents
Culture Documents
Big Integer in Java
Big Integer in Java
Big Integer in Java
You can use the BigInteger class for integers and BigDecimal for
numbers with decimal digits. Both classes are defined in java.math
package.
Example:
BigInteger(byte[])
BigInteger(int, byte[])
Returns a randomly selected BigInteger with the specified bitLength that is probably prime.
BigInteger(int, Random)
BigInteger(String)
Translates a string containing an optional minus sign followed by a sequence of one or more
decimal digits into a BigInteger.
BigInteger(String, int)
Translates a string containing an optional minus sign followed by a sequence of one or more
digits in the specified radix into a BigInteger.
abs()
add(BigInteger)
Returns a BigInteger whose value is (this + val).
and(BigInteger)
andNot(BigInteger)
bitCount()
Returns the number of bits in the two's complement representation of this number that
differ from its sign bit.
bitLength()
Returns the number of bits in the minimal two's-complement representation of this number,
*excluding* a sign bit, i.e., (ceil(log2(this < 0 ? -this : this + 1))).
clearBit(int)
Returns a BigInteger whose value is equivalent to this number with the designated bit
cleared.
compareTo(BigInteger)
Returns -1, 0 or 1 as this number is less than, equal to, or greater than val.
divide(BigInteger)
divideAndRemainder(BigInteger)
doubleValue()
equals(Object)
flipBit(int)
Returns a BigInteger whose value is equivalent to this number with the designated bit
flipped.
floatValue()
Returns a BigInteger whose value is the greatest common denominator of abs(this) and
abs(val).
getLowestSetBit()
Returns the index of the rightmost (lowest-order) one bit in this number (i.e., the number of
zero bits to the right of the rightmost one bit).
hashCode()
intValue()
isProbablePrime(int)
Returns true if this BigInteger is probably prime, false if it's definitely composite.
longValue()
max(BigInteger)
Returns the BigInteger whose value is the greater of this and val.
min(BigInteger)
Returns the BigInteger whose value is the lesser of this and val.
mod(BigInteger)
modInverse(BigInteger)
modPow(BigInteger, BigInteger)
multiply(BigInteger)
negate()
or(BigInteger)
pow(int)
remainder(BigInteger)
setBit(int)
Returns a BigInteger whose value is equivalent to this number with the designated bit set.
shiftLeft(int)
shiftRight(int)
signum()
Returns the signum function of this number (i.e., -1, 0 or 1 as the value of this number is
negative, zero or positive).
subtract(BigInteger)
testBit(int)
toByteArray()
toString()
toString(int)
valueOf(long)
Returns a BigInteger with the specified value.
xor(BigInteger)
Class java.math.BigDecimal
ROUND_CEILING
ROUND_DOWN
ROUND_FLOOR
ROUND_HALF_DOWN
Behave as for ROUND_UP if the discarded fraction is > .5; otherwise, behave as for
ROUND_DOWN.
ROUND_HALF_EVEN
Behave as for ROUND_HALF_UP if the digit to the left of the discarded fraction is odd;
behave as for ROUND_HALF_DOWN if it's even.
ROUND_HALF_UP
Behave as for ROUND_UP if the discarded fraction is >= .5; otherwise, behave as for
ROUND_DOWN.
ROUND_UNNECESSARY
ROUND_UP
BigDecimal(BigInteger, int)
BigDecimal(double)
BigDecimal(String)
abs()
Returns a BigDecimal whose value is the absolute value of this number, and whose scale is
this.scale().
add(BigDecimal)
Returns a BigDecimal whose value is (this + val), and whose scale is MAX(this.scale(),
val.scale).
compareTo(BigDecimal)
Returns -1, 0 or 1 as this number is less than, equal to, or greater than val.
divide(BigDecimal, int)
Returns a BigDecimal whose value is (this / val), and whose scale is this.scale().
Returns a BigDecimal whose value is (this / val), and whose scale is as specified.
doubleValue()
equals(Object)
floatValue()
Converts this number to a float.
hashCode()
intValue()
longValue()
max(BigDecimal)
Returns the BigDecimal whose value is the greater of this and val.
min(BigDecimal)
Returns the BigDecimal whose value is the lesser of this and val.
movePointLeft(int)
Returns a BigDecimal which is equivalent to this one with the decimal point moved n places
to the left.
movePointRight(int)
Moves the decimal point the specified number of places to the right.
multiply(BigDecimal)
Returns a BigDecimal whose value is (this * val), and whose scale is this.scale() + val.scale.
negate()
scale()
setScale(int)
Returns a BigDecimal whose scale is the specified value, and whose value is exactly equal to
this number's.
setScale(int, int)
Returns a BigDecimal whose scale is the specified value, and whose integer value is
determined by multiplying or dividing this BigDecimal's integer value by the appropriate
power of ten to maintain the overall value.
signum()
Returns the signum function of this number (i.e., -1, 0 or 1 as the value of this number is
negative, zero or positive).
subtract(BigDecimal)
Returns a BigDecimal whose value is (this - val), and whose scale is MAX(this.scale(),
val.scale).
toBigInteger()
toString()
valueOf(long)
valueOf(long, int)
In this example, the variable x is an int and Java will initialize it to 0 for you. When you
assign it the value of 10 on the second line, your value of 10 is written into the memory
location referred to by x.
But, when you try to declare a reference type, something different happens. Take the
following code:
Integer num;
num = new Integer(10);
The first line declares a variable named num, but it does not actually contain a primitive value
yet. Instead, it contains a pointer (because the type is Integer which is a reference type).
Since you have not yet said what to point to, Java sets it to null, which means "I am
pointing to nothing".
In the second line, the new keyword is used to instantiate (or create) an object of type
Integer and the pointer variable num is assigned to that Integer object.
The NullPointerException occurs when you declare a variable but did not create an object.
So you are pointing to something that does not actually exist.