【java.math.BigInteger】【转】常见问题
好大的链接给原作
Q: 在java怎样将BigInteger类型的数据转成int类型的?
A:BigInteger的intValue()可以获得int类型数值。
Q: java.math.BigInteger有位数限制么?比如long是2的64次方。
A:从BigInteger的源码可以看出来,在BigInteger内部数值是通过:int[] mag存放数据的,总共可以存放2147483647个int数据,而每个int数据由4个字节表示,所以BigInteger理论上可以存放的数据最大为22147483647*4*8-1-1
Q: 如何生成一个随机的大数据?
A:利用BigInteger的构造函数:BigInteger(int numBits, Random rnd)构造一个随机产生的大整数,范围在0到2^numBits – 1之间.
Random r = new Random();
BigInteger data = new BigInteger(100,r);
System.out.println("data:"+data)
Q: BigInteger的四则运算实现过程是什么样的?
A:
1、add方法和subtract方法实际上进行的是数组对位相加和相减,这个过程用私有的函数来完成,返回的是为此重新分配空间的结果数组索引值(数组首地址)。然后再用结果数组来构造一个BigInteger作为最后的返回值。
2、multiply方法使用的是数组相乘,用数组来模拟数字相乘,同样使用一个私有的函数来完成这个过程,为相乘结果数组分配新的空间,最后用结果数组来构造一个BigInteger 作为返回值。
3、divide方法实现使用的是MutableBigInteger类,MutableBigInteger类不是公开的,它只供
java.math类库内部使用。MutableBigInteger是BigInteger类的另一个版本,它的特点是不创建临时对象的前提上使调用程
序得到象BigInteger类型的返回值(称为可变对象技术)。因为大整数的除法是由大量的其他算术操作组成的,所以需要大量的临时对象,而完成大量的操作而不创建新的对象可以极大地改善程序的性能,(因为创建对象的代价是很高的)所以在Java的大整数类中使用MutableBigInteger类中
的方法来执行大整数除法。MutableBigInteger类包含在java.math包中,BigInteger类的divide方法使用了
MutableBigInteger类的除法运算所返回的结果,使用这个MutableBigInteger类型的结果来构造一个BigInteger。
实际上在MutableBigInteger类中也使用了数组来模拟数字相除。
Q: BigInteger为什么设计成不可变的类,是在进行每一步运算时,都会产生一个新的对象的吗?
A:虽然BigInteger被设计成一个不可变的类,但是并不是每一步运算都生成一个新的对象,可以看下面的源码:
public BigInteger add(BigInteger val)
{
int[] resultMag;
if (val.signum == 0)
{
return this;
}
if (signum == 0)
{
return val;
}
if (val.signum == signum)
{
return new BigInteger(add(mag, val.mag), signum);
}
int cmp = intArrayCmp(mag, val.mag);
if (cmp==0)
{
return ZERO;
}
resultMag = (cmp>0 ? subtract(mag, val.mag) : subtract(val.mag, mag));
resultMag = trustedStripLeadingZeroInts(resultMag);
return new BigInteger(resultMag, cmp*signum);
}
Q: 在java中 有没有比BigInteger范围更大的?
A: 在java中没有比BigInteger范围更大数了。BigInteger类可以处理包含任意长度数字序列的数值,因为在BigInteger内部是通 过int 数组来表示和处理大数据的,int类型的最大值是2147483647,所以int数组最多有21亿个int值,这些数值足够大的,已经满足了超大数据的使用。
Q: 在哪里可以查看BigInteger的代码实现?
A: 在jdk里面的java.math包下面就可以看到
【java.math.BigInteger】【转】常见问题的更多相关文章
- 【java.math.BigInteger】常用函数
1. /* 返回此BigInteger的函数正负号. 此方法返回-1,0或1作为此BigInteger的值对应是负,零或正数. */ java.math.BigInteger.signum(BigIn ...
- [记录]java.math.biginteger cannot be cast to java.lang.long
可以直接使用BigInteger类型进行接收, BigInteger id = (BigInteger)QueryRunner(conn,"SELECT LAST_INSERT_ID&quo ...
- java.math.BigInteger使用心得总结(转)
今天参考课本写了一个关于二进制与十进制转换的程序,程序算法不难,但写完后测试发现不论是二转十还是十转二,对于大于21亿即超过整数范围的数不能很好的转换.都会变成0.参考书籍发现使用使用BigInteg ...
- Java中的java.math.BigInteger
Java中的java.math.BigInteger /** * */ package com.you.model; /** * @author YouHaidong * */ public clas ...
- mysql连接报java.math.BigInteger cannot be cast to java.lang.Long异常
使用hibernate出现以下错误 java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot b ...
- 【问题解决:连接异常】 java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
问题描述: MySQL更新到8.0.11之后连接数据库时会报出错误 Your login attempt was not successful, try again.Reason: Could not ...
- 连接Mysql时出现java.math.BigInteger cannot be cast to java.lang.Long问题
今天遇见这样一个坑.在连接数据库进行查询数据时,大家可能会遇见这样一个问题:java.math.BigInteger cannot be cast to java.lang.Long,然后去检查代码中 ...
- Spring boot启动时报 java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long错误
Spring boot启动时报 java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be ...
- 初学MyBatis(踩坑)Error querying database. Cause: java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
最近在学习Mybatis,代码全部根据教程写好了,一运行结果报了一个错误,主要错误内容: Caused by: org.apache.ibatis.exceptions.PersistenceExce ...
随机推荐
- angular select中ng-options使用
function selectCtrl($scope) { $scope.selected = ''; $scope.model = [{ id: 10001, mainCategory: '男', ...
- css 关于两栏布局,左边固定,右边自适应
好几个星期都没写博客了,最近不忙也不闲,稀里糊涂过了两个星期,之前几个月内天天坚持签到.最近也没签到.哈哈,说正事. 今天做东钿互金平台后台页面,昨天做了一个登录页面,业偶碰到了一个难题.等下也要把它 ...
- CentOS6 下Vim安装和配置
1.系统只安装了vim-minimal,执行以下命令安装另外两个组件 yum install vim-common yum install vim-enhanced 2.安装ctags yum ins ...
- C#实现XML文件数据库存储
C#实现文件数据库 http://www.cnblogs.com/gaochundong/archive/2013/04/24/csharp_file_database.html#3100076 应用 ...
- CMSIS Example - osTimer osTimerCreate osTimerStart
osTimerId timer; uint32_t cnt=; void timerHandler( void * arg ) { cnt++; osTimerStart( timer, ); } o ...
- C++ delete operator做了什么事
1.C++中的delete operator做了两件事:调用析构方法和调用operator delete释放内存. 2.考虑析构方法,如果析构方法是虚方法,调用指针真实类型的析构方法,否则调用表面类型 ...
- 字符集乱码问题:ISO-8859-1和GBK
问题,引用百度知道的问题吧: http://zhidao.baidu.com/question/51342167.html?qbl=relate_question_0&word=%C3%84% ...
- 五 Django 1.5.4 User Authentication 用户认证
一.创建drinker app ./manage.py startapp drinker 在INSTALL_APPS添加drinker 用户的Profile模型,django里面是可以自定义的. 通过 ...
- volatile synschonized的区别
在一次面试中,被问到volatile与synschonized的区别,概念模模糊糊,今天做一个总结,加强自己的认识. 本文参考http://www.cnblogs.com/dolphin0520/p/ ...
- C# 网络通信大小端转换类
本篇文章主要介绍了"C# 网络通信大小端转换类" using System;namespace Framework.NetPackage.Common { /// <summ ...