BigInteger类方法代码 详解 简介
懒得做PPT了,直接写代码。
import java.math.BigInteger;
import java.util.Random;
public class Biginteger {
public static void main(String[] args) {
/** @实现
* public class BigInteger extends Number implements Comparable<BigInteger>{
* final int signum;
* final int[] mag;
* }
*/
/** @范围测试
* >10^83886081 test.txt: 80.01MB time: 578174ms
*
* BigInteger s = new BigInteger("10000000000");
* long t = System.currentTimeMillis();
* for(int i=0;i<1000000;i++){
* s = s.multiply(s);
* System.out.println(s.toString().length());
* System.out.println(System.currentTimeMillis()-t);
* }
*/
/** 常量
* @public static final BigInteger ZERO
* @public static final BigInteger ONE
* public static final BigInteger TWO (jdk9)
* public static final BigInteger TEN
*/
System.out.println("常数:" + BigInteger.ZERO + "," + BigInteger.ONE + "," + BigInteger.TEN + '\n');
/** 构造
* @public BigInteger(String val)
* @public BigInteger(String val, int radix)
* public BigInteger(byte[] val) public BigInteger(int signum, byte[]magnitude)
* public BigInteger(int bitLength, int certainty, Random rnd)
* public BigInteger(int numBits, Random rnd) public BigInteger(String val)
*/
// 使用字符串构造,默认为十进制。使用字符串构造需要注意数值必须为正
System.out.println("字符串构造大数:" + new BigInteger("+123123123123123"));
System.out.println("int等类型构造大数:" + new BigInteger(String.valueOf(-1234567890)));
System.out.println("指定进制字符串构造大数:" + new BigInteger("ZZZZZZZ", 36));
byte[] by = {
15, 15 };
// 00001111 00001111 -> 3855
System.out.println("按位构造:" + new BigInteger(by));
System.out.println("按位构造(有符号):" + new BigInteger(-1, by)