If the precision of the basic integer and floating-point types is not sufficient, you can turn to a couple of handy classes in the java.math package: BigInteger and BigDecimal. These are classes for manipulating numbers with an arbitrarily long sequence of digits. The BigInteger class implements arbitrary precision integer arithmetic, and BigDecimal does the same for floating-point numbers.
Use the static valueOf method to turn an ordinary number into a big number:
BigInteger a = BigInteger.valueOf(100); Unfortunately, you cannot use the familiar mathematical operators such as + and * to combine big numbers. Instead, you must use methods such as add and multiply in the big number classes. BigInteger c = a.add(b); // c = a + bBigInteger d = c.multiply(b.add(BigInteger.valueOf(2))); // d = c * (b + 2) API:java.math.BigInteger 1.1BigInteger add(BigInteger other)
BigInteger subtract(BigInteger other)
BigInteger multiply(BigInteger other)
BigInteger divide(BigInteger other)
BigInteger mod(BigInteger other)
return the sum, difference, product, quotient, and remainder of this big integer and other.
int compareTo(BigInteger other)
returns 0 if this big integer equals other, a negative result if this big integer is less than other, and a positive result otherwise.
static BigInteger valueOf(long x)
returns a big integer whose value equals x.
读完以上内容,发现自己以前对BigInteger的使用并不理想,譬如关于将一个普通数字转换为大数字,只知道用BigInteger的构造方法实现,将数转化为字符串,再构造BigInteger,commits a folly!,实际上用valueOf轻松可以实现,唉……P.S. 使用大数前,切记import java.math.*;