BigInteger、BigDecimal类的使用详解
我们都知道在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类的使用详解的更多相关文章
- 【转】UML类图与类的关系详解
UML类图与类的关系详解 2011-04-21 来源:网络 在画类图的时候,理清类和类之间的关系是重点.类的关系有泛化(Generalization).实现(Realization).依赖(D ...
- String类的构造方法详解
package StringDemo; //String类的构造方法详解 //方法一:String(); //方法二:String(byte[] bytes) //方法三:String (byte[] ...
- [转]c++类的构造函数详解
c++构造函数的知识在各种c++教材上已有介绍,不过初学者往往不太注意观察和总结其中各种构造函数的特点和用法,故在此我根据自己的c++编程经验总结了一下c++中各种构造函数的特点,并附上例子,希望对初 ...
- Scala 深入浅出实战经典 第63讲:Scala中隐式类代码实战详解
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...
- UML类图与类的关系详解
摘自:http://www.uml.org.cn/oobject/201104212.asp UML类图与类的关系详解 2011-04-21 来源:网络 在画类图的时候,理清类和类之间的关系是重点.类 ...
- phpcms加载系统类与加载应用类之区别详解
<?php 1. 加载系统类方法load_sys_class($classname, $path = ''", $initialize = 1)系统类文件所在的文件路径:/phpcms ...
- c++类的构造函数详解
c++类的构造函数详解 一. 构造函数是干什么的 class Counter{ public: // 类Counter的构造函数 // 特点:以类名作为函数名,无返回类 ...
- 对python3中pathlib库的Path类的使用详解
原文连接 https://www.jb51.net/article/148789.htm 1.调用库 ? 1 from pathlib import 2.创建Path对象 ? 1 2 3 4 5 ...
- String类内存空间详解
java.lang.String类内存问题详解 字符串理解的难点在于其在堆内存空间上的特殊性,字符串String对象在堆内存上有两种空间: 字符串池(String pool):特殊的堆内存,专门存放S ...
随机推荐
- [LNOI2014]LCA(树链剖分)
BZOJ传送门 Luogu传送门 题目:给你一棵树,给你n个询问,每个询问要求输出$\sum_{i=l}^{r}depth(LCA(i,z))$ 细看看其实没有想象的那么难 大体思路: 1.对于每个询 ...
- 本地搭建easy-mock
easy-mock要用nodejs启动,需要先安装nodejs ubuntu系统: apt install node centos系统: curl --silent --location https: ...
- Import CAD geometry in BladeModeler, turbogrid
Table of Contents 1. Import CAD geometry in BladeModeler, turbogrid 1 Import CAD geometry in BladeMo ...
- 自己动手编写vue插件
一.为什么要自己动手写插件呢,原因有二: 其一:是因为最近产品了提了一个在web端接收,消息通知的需求,产品要求在若干个页面内如果有消息,就要弹出消息弹窗展示给用户,略加思索之后,第一反应就是写个消息 ...
- ssh登录问题
ssh username@ip 密码正确但是登陆ssh时permission denied 1. 启动sshd:/etc/init.d/ssh start 2. 在/etc/ssh/sshd_ ...
- [bzoj1356]Rectangle[Baltic2009][几何常识乱搞]
虽然说是几何常识乱搞,但是想不到啊.. 题意:n个点取4个组成矩形,使面积最大,求面积. n<=1500 题解: 1.对角线相等且相互交于中点的四边形是矩形. 2.矩形四点共圆. 所以$n^2$ ...
- 交友app
编辑注记:这是由译者 han_qi 翻译纽约客的一篇文章,从女性的角度描写了交友产品的用户体验及需求,值得广大产品经理深入研究,文章略长,但值得深读.原文<Overwhelmed and Cre ...
- Oracle怎么用(常用工具)
Oracle数据库管理系统装好了!那要怎么用呢? 将介绍的工具:①Database Configuration Assistant ②SQL Plus ③SQL Developer 一.Dat ...
- Ubuntu 16.04安装迅雷(兼容性不高)
迅雷官方没有提供LInux的版本,但是提供了一个Xware的版本,这个是用来制作离线下载的,但是网上已经有人通过这个集成了桌面应用:但是没怎么测试过,稳定性不高. http://forum.ubunt ...
- Nexus设备升级5.0方法
1. 从该页面为您的设备下载适当的系统映像.然后将它解压缩到一个安全的文件夹. 2. 通过 USB 连接到您的计算机. 3. 使用下列的方法,在fastboot mode下启动设备: 使用 adb ...