我们都知道在java里边long算是存储长度比较大的了,但是如果有很大的数我们应该怎么处理呢,不用怕,java还为我们准备了一个BigInteger的类,那么这个类到底能存储多大的数呢,这个一时还真不好想,要取决于你计算机的内存大小,意味着我们的内存越大,这个类存储的位数就越大。接下来我们来看看BigInteger这个类。

BigInteger继承了Number类并且实现了Serializable , Comparable < BigInteger >接口

首先来看他的构造函数

这个类与之对应的还有三个常量,我们来看下

接下来我们说下他常用的构造函数

比较常用的就是用字符串来直接传入
BigInteger bigInteger = new BigInteger("999999999999999999");

比较常用的成员方法有如下:

BigInteger abs()  返回大整数的绝对值
BigInteger add(BigInteger val) 返回两个大整数的和
BigInteger and(BigInteger val) 返回两个大整数的按位与的结果
BigInteger andNot(BigInteger val) 返回两个大整数与非的结果
BigInteger divide(BigInteger val) 返回两个大整数的商
double doubleValue() 返回大整数的double类型的值
float floatValue() 返回大整数的float类型的值
BigInteger gcd(BigInteger val) 返回大整数的最大公约数
int intValue() 返回大整数的整型值
long longValue() 返回大整数的long型值
BigInteger max(BigInteger val) 返回两个大整数的最大者
BigInteger min(BigInteger val) 返回两个大整数的最小者
BigInteger mod(BigInteger val) 用当前大整数对val求模
BigInteger multiply(BigInteger val) 返回两个大整数的积
BigInteger negate() 返回当前大整数的相反数
BigInteger not() 返回当前大整数的非
BigInteger or(BigInteger val) 返回两个大整数的按位或
BigInteger pow(int exponent) 返回当前大整数的exponent次方
BigInteger remainder(BigInteger val) 返回当前大整数除以val的余数
BigInteger leftShift(int n) 将当前大整数左移n位后返回
BigInteger rightShift(int n) 将当前大整数右移n位后返回
BigInteger subtract(BigInteger val)返回两个大整数相减的结果
byte[] toByteArray(BigInteger val)将大整数转换成二进制反码保存在byte数组中
String toString() 将当前大整数转换成十进制的字符串形式
BigInteger xor(BigInteger val) 返回两个大整数的异或

我们来看几个测试

public static void main(String[] args) {

		BigInteger b1 = new BigInteger("999999999999999999999");
BigInteger b2 = new BigInteger("888888888888888888888"); //加法
System.out.println(b1.subtract(b2));
//减法
System.out.println(b1.add(b2));
//乘法
System.out.println(b1.multiply(b2));
//除法
System.out.println(b1.divide(b2));
//取余
System.out.println(b1.remainder(b2));
//除并取余
BigInteger[] bbs = b1.divideAndRemainder(b2);
System.out.println(bbs[0]+"--"+bbs[1]);
}

程序的运行结果如下:

111111111111111111111
1888888888888888888887
888888888888888888887111111111111111111112
1
111111111111111111111
1--111111111111111111111

接下来我带大家看下BigInteger底层的源码

这个this是什么东西呢,我们继续往下看

/**
* Translates the String representation of a BigInteger in the
* specified radix into a BigInteger. The String representation
* consists of an optional minus or plus sign followed by a
* sequence of one or more digits in the specified radix. The
* character-to-digit mapping is provided by {@code
* Character.digit}. The String may not contain any extraneous
* characters (whitespace, for example).
*
* @param val String representation of BigInteger.
* @param radix radix to be used in interpreting {@code val}.
* @throws NumberFormatException {@code val} is not a valid representation
* of a BigInteger in the specified radix, or {@code radix} is
* outside the range from {@link Character#MIN_RADIX} to
* {@link Character#MAX_RADIX}, inclusive.
* @see Character#digit
*/
public BigInteger(String val, int radix) { //首先这里有一个基数因子 是指当前是多少进制的
int cursor = 0, numDigits;
final int len = val.length(); //这个地方会获取到当前传入字符串的总长度 //这里有两个常量 MIN_RADIX = 2 , MAX_RADIX = 36
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
throw new NumberFormatException("Radix out of range");
if (len == 0)
throw new NumberFormatException("Zero length BigInteger"); // Check for at most one leading sign
//这里会检测传入的字符串是否是纯数字
int sign = 1;
int index1 = val.lastIndexOf('-');
int index2 = val.lastIndexOf('+');
if (index1 >= 0) {
if (index1 != 0 || index2 >= 0) {
throw new NumberFormatException("Illegal embedded sign character");
}
sign = -1;
cursor = 1;
} else if (index2 >= 0) {
if (index2 != 0) {
throw new NumberFormatException("Illegal embedded sign character");
}
cursor = 1;
}
if (cursor == len)
throw new NumberFormatException("Zero length BigInteger"); // Skip leading zeros and compute number of digits in magnitude while (cursor < len &&
Character.digit(val.charAt(cursor), radix) == 0) {
cursor++;
} if (cursor == len) {
signum = 0;
mag = ZERO.mag;
return;
} numDigits = len - cursor;
signum = sign; // Pre-allocate array of expected size. May be too large but can
// never be too small. Typically exact.
long numBits = ((numDigits * bitsPerDigit[radix]) >>> 10) + 1;
if (numBits + 31 >= (1L << 32)) {
reportOverflow();
}
int numWords = (int) (numBits + 31) >>> 5;
int[] magnitude = new int[numWords]; // Process first (potentially short) digit group
int firstGroupLen = numDigits % digitsPerInt[radix];
if (firstGroupLen == 0)
firstGroupLen = digitsPerInt[radix];
String group = val.substring(cursor, cursor += firstGroupLen);
magnitude[numWords - 1] = Integer.parseInt(group, radix);
if (magnitude[numWords - 1] < 0)
throw new NumberFormatException("Illegal digit"); // Process remaining digit groups
int superRadix = intRadix[radix];
int groupVal = 0;
while (cursor < len) {
group = val.substring(cursor, cursor += digitsPerInt[radix]);
groupVal = Integer.parseInt(group, radix);
if (groupVal < 0)
throw new NumberFormatException("Illegal digit");
destructiveMulAdd(magnitude, superRadix, groupVal);
}
// Required for cases where the array was overallocated.
mag = trustedStripLeadingZeroInts(magnitude);
if (mag.length >= MAX_MAG_LENGTH) {
checkRange();
}
}

看了以上的代码,相信你对BigInteger大概已经有了一个新的认识。

以上内容部分来自网络,与BigInteger对应的还有一个BigDecimal,可以存小数,与BigInteger的用法一模一样,有问题可以在下面评论,技术问题可以私聊我。

BigInteger、BigDecimal类的使用详解的更多相关文章

  1. 【转】UML类图与类的关系详解

    UML类图与类的关系详解   2011-04-21 来源:网络   在画类图的时候,理清类和类之间的关系是重点.类的关系有泛化(Generalization).实现(Realization).依赖(D ...

  2. String类的构造方法详解

    package StringDemo; //String类的构造方法详解 //方法一:String(); //方法二:String(byte[] bytes) //方法三:String (byte[] ...

  3. [转]c++类的构造函数详解

    c++构造函数的知识在各种c++教材上已有介绍,不过初学者往往不太注意观察和总结其中各种构造函数的特点和用法,故在此我根据自己的c++编程经验总结了一下c++中各种构造函数的特点,并附上例子,希望对初 ...

  4. Scala 深入浅出实战经典 第63讲:Scala中隐式类代码实战详解

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  5. UML类图与类的关系详解

    摘自:http://www.uml.org.cn/oobject/201104212.asp UML类图与类的关系详解 2011-04-21 来源:网络 在画类图的时候,理清类和类之间的关系是重点.类 ...

  6. phpcms加载系统类与加载应用类之区别详解

    <?php 1. 加载系统类方法load_sys_class($classname, $path = ''", $initialize = 1)系统类文件所在的文件路径:/phpcms ...

  7. c++类的构造函数详解

    c++类的构造函数详解 一. 构造函数是干什么的 class Counter{ public:         // 类Counter的构造函数         // 特点:以类名作为函数名,无返回类 ...

  8. 对python3中pathlib库的Path类的使用详解

    原文连接   https://www.jb51.net/article/148789.htm 1.调用库 ? 1 from pathlib import 2.创建Path对象 ? 1 2 3 4 5 ...

  9. String类内存空间详解

    java.lang.String类内存问题详解 字符串理解的难点在于其在堆内存空间上的特殊性,字符串String对象在堆内存上有两种空间: 字符串池(String pool):特殊的堆内存,专门存放S ...

随机推荐

  1. Radar Installation POJ - 1328 (贪心)

    题目大意(vj上的翻译版本) 假定海岸线是无限长的直线.陆地位于海岸线的一侧,海洋位于另一侧.每个小岛是位于海洋中的一个点.对于任何一个雷达的安装 (均位于海岸线上),只能覆盖 d 距离,因此海洋中的 ...

  2. Oracle 数据库启动与关闭 各种方式详解整理

    概述 只有具备sysdba和sysoper系统特权的用户才能启动和关闭数据库. 在启动数据库之前应该启动监听程序,否则就不能利用命令方式来管理数据库,包括启动和关闭数据库. 虽然数据库正常运行,但如果 ...

  3. 阿里云安装nodejs

    cd进入root目录下: cd /root 下载node.js安装包 wget https://nodejs.org/dist/v8.11.2/node-v8.11.2-linux-x64.tar.x ...

  4. vue中的跨域

    proxyTable: { '/zabbix_api': { target: 'http://10.88.22.8',//设置你调用的接口域名和端口号 别忘了加http changeOrigin: t ...

  5. LINUX应用开发工程师职位(含答案)

    就业模拟测试题-LINUX应用开发工程师职位 本试卷从考试酷examcoo网站导出,文件格式为mht,请用WORD/WPS打开,并另存为doc/docx格式后再使用 试卷编号:143989试卷录入者: ...

  6. HDU 5291 Candy Distribution

    Candy Distribution Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  7. noip模拟赛 遭遇

    分析:暴力挺好打的,对于前30%的数据神搜,hi相同的数据将所有的建筑按照c从小到大排序,看最多能跳多少,ci=0的数据将所有的建筑按照h从小到大排序,枚举起点和终点,看能否跳这么多,取个max就可以 ...

  8. ORA-12541:TNS:无监听程序

    安装oracle以后,sql plus可以正常登陆,pl/sql登陆时报错ORA-12541:TNS:无监听程序,解决方案如下: http://blog.csdn.net/hao134838/arti ...

  9. multiple instance of mac app

    一般情况下,mac系统上的应用程序只能启动一个实例,现在做项目,需要mac上同时启动多个实例,如何做呢,下面就说明完成这个功能的方法: 主要原理:利用 open -n Applications/XXX ...

  10. 在Electron中通过ffi模块实现JavaScript调用C++动态库

    目前在网上能搜到的JS调C++动态库的实现有两种,一种是通过开发Node.js addon模块的方式实现调用,这种对于我们已有的代码实现比较复杂,需要大量的开发,因此不适用:另一种是通过FFI模块,F ...