在Java中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,理论上能够表示无线大的数,只要计算机内存足够大。

这两个类都在 java.math.* 包中,因此每次必须在开头处引用该包。

构造方法:

BigInteger(byte[] val)
    将包含 BigInteger 的二进制补码表示形式的 byte 数组转换为 BigInteger。
BigInteger(int signum, byte[] magnitude)
    将 BigInteger 的符号-数量表示形式转换为 BigInteger。

 import java.math.BigInteger;

 public class Main {
public static void main(String[] args) {
byte []bytes = new byte[]{1,2} ;
BigInteger bigInteger = new BigInteger(bytes) ;
// 将BigInteger的符号-数量表示形式转换为 BigInteger。
// 该符号表示为一个正负号整数值:-1 表示负,0 表示零,1 表示正。
// 该大小是一个 big-endian 字节顺序的 byte 数组:最高有效字节在第零个元素中。
// 允许零长度数量数组,这会导致 BigInteger 的值为 0,无论其正负号是 -1、0 还是 1。
// 参数:
// signum - 该数的正负号(-1 表示负,0 表示零,1 表示正)。
// magnitude - 该数的大小的 big-endian 二进制表示形式。
System.out.println(bigInteger);
}
}

BigInteger(int bitLength, int certainty, Random rnd)
    构造一个随机生成的正 BigInteger,它可能是一个具有指定 bitLength 的素数。
BigInteger(int numBits, Random rnd)
    构造一个随机生成的 BigInteger,它是在 0 到 (2^numBits - 1)(包括)范围内均匀分布的值。

 import java.math.BigInteger;
import java.util.Random; public class Main {
public static void main(String[] args) {
Random r = new Random() ;
// 随机素数
BigInteger bigInteger = new BigInteger(5,20,r);
//certainty 代表是素数的概率,越大是素数的概率越小 1-1/2^certainty
//bitLength 代表bit长度
//比如本例输出为23,二进制表示为11101
//5位最大表示为25,所以最大为25
System.out.println(bigInteger);
// 随机数
BigInteger bigInteger1 = new BigInteger(10,r);
// 0 到 (2^numBits - 1) 范围的随机数
System.out.println(bigInteger1);
}
}

BigInteger(String val)
    将 BigInteger 的十进制字符串表示形式转换为 BigInteger。
BigInteger(String val, int radix)
    将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger。

 import java.math.BigInteger;

 public class Main {
public static void main(String[] args) {
BigInteger bigInteger = new BigInteger("123456789") ;
System.out.println(bigInteger);
BigInteger bigInteger1 = new BigInteger("111110",2) ;
// 将基数为2的二进制数(111110)转换为BigInteger的10进制类型
System.out.println(bigInteger1);
}
}

BigIntegerd的方法摘要:

BigInteger abs()
  返回其值是此 BigInteger 的绝对值的 BigInteger。

 import java.math.BigInteger;

 public class Main {
public static void main(String[] args) {
BigInteger bigInteger = new BigInteger("-123456789") ;
System.out.println(bigInteger);
bigInteger = bigInteger.abs();
System.out.println(bigInteger);
}
}
/*
-123456789
123456789
Process finished with exit code 0
*/

BigInteger add(BigInteger val)
  返回其值为 (this + val) 的 BigInteger。

 import java.math.BigInteger;

 public class Main {
public static void main(String[] args) {
BigInteger bigInteger = new BigInteger("123456789") ;
bigInteger = bigInteger.add(new BigInteger("12312312312"));
System.out.println(bigInteger);
}
}
/*
12435769101 Process finished with exit code 0
*/

BigInteger and(BigInteger val)
  返回其值为 (this & val) 的 BigInteger。

BigInteger andNot(BigInteger val)
  返回其值为 (this & ~val) 的 BigInteger。
int bitCount()
  返回此 BigInteger 的二进制补码表示形式中与符号不同的位的数量。
int bitLength()
  返回此 BigInteger 的最小的二进制补码表示形式的位数,不包括 符号位。
BigInteger clearBit(int n)
  返回其值与清除了指定位的此 BigInteger 等效的 BigInteger。
int compareTo(BigInteger val)
  将此 BigInteger 与指定的 BigInteger 进行比较。
BigInteger divide(BigInteger val)
  返回其值为 (this / val) 的 BigInteger。
BigInteger[] divideAndRemainder(BigInteger val)
  返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。
double doubleValue()
  将此 BigInteger 转换为 double。
boolean equals(Object x)
  比较此 BigInteger 与指定的 Object 的相等性。
BigInteger flipBit(int n)
  返回其值与对此 BigInteger 进行指定位翻转后的值等效的 BigInteger。
float floatValue()
  将此 BigInteger 转换为 float。
BigInteger gcd(BigInteger val)
  返回一个 BigInteger,其值是 abs(this) 和 abs(val) 的最大公约数。
int getLowestSetBit()
  返回此 BigInteger 最右端(最低位)1 比特的索引(即从此字节的右端开始到本字节中最右端 1 比特之间的 0 比特的位数)。
int hashCode()
  返回此 BigInteger 的哈希码。
int intValue()
  将此 BigInteger 转换为 int。
boolean isProbablePrime(int certainty)
  如果此 BigInteger 可能为素数,则返回 true,如果它一定为合数,则返回 false。
long longValue()
  将此 BigInteger 转换为 long。
BigInteger max(BigInteger val)
  返回此 BigInteger 和 val 的最大值。
BigInteger min(BigInteger val)
  返回此 BigInteger 和 val 的最小值。
BigInteger mod(BigInteger m)
  返回其值为 (this mod m) 的 BigInteger。
BigInteger modInverse(BigInteger m)
  返回其值为 (this-1 mod m) 的 BigInteger。
BigInteger modPow(BigInteger exponent, BigInteger m)
  返回其值为 (thisexponent mod m) 的 BigInteger。
BigInteger multiply(BigInteger val)
  返回其值为 (this * val) 的 BigInteger。
BigInteger negate()
  返回其值是 (-this) 的 BigInteger。
BigInteger nextProbablePrime()
  返回大于此 BigInteger 的可能为素数的第一个整数。
BigInteger not()
  返回其值为 (~this) 的 BigInteger。
BigInteger or(BigInteger val)
  返回其值为 (this | val) 的 BigInteger。
BigInteger pow(int exponent)
  返回其值为 (thisexponent) 的 BigInteger。
static BigInteger probablePrime(int bitLength, Random rnd)
  返回有可能是素数的、具有指定长度的正 BigInteger。
BigInteger remainder(BigInteger val)
  返回其值为 (this % val) 的 BigInteger。
BigInteger setBit(int n)
  返回其值与设置了指定位的此 BigInteger 等效的 BigInteger。
BigInteger shiftLeft(int n)
  返回其值为 (this << n) 的 BigInteger。
BigInteger shiftRight(int n)
  返回其值为 (this >> n) 的 BigInteger。
int signum()
  返回此 BigInteger 的正负号函数。
BigInteger subtract(BigInteger val)
  返回其值为 (this - val) 的 BigInteger。
boolean testBit(int n)
  当且仅当设置了指定的位时,返回 true。
byte[] toByteArray()
  返回一个 byte 数组,该数组包含此 BigInteger 的二进制补码表示形式。
String toString()
  返回此 BigInteger 的十进制字符串表示形式。
String toString(int radix)
  返回此 BigInteger 的给定基数的字符串表示形式。
static BigInteger valueOf(long val)
  返回其值等于指定 long 的值的 BigInteger。
BigInteger xor(BigInteger val)
  返回其值为 (this ^ val) 的 BigInteger。

BigInteger详解的更多相关文章

  1. Java BigInteger详解

    BigInteger概述 可用于无限大的整数计算 所在的包 java.math.BigInteger; 构造函数 public BigInteger(String val) 成员函数 比较大小函数 p ...

  2. 深入浅出Mybatis系列(四)---配置详解之typeAliases别名(mybatis源码篇)

    上篇文章<深入浅出Mybatis系列(三)---配置详解之properties与environments(mybatis源码篇)> 介绍了properties与environments, ...

  3. 多线程之 Final变量 详解

    原文: http://www.tuicool.com/articles/2Yjmqy 并发编程网:http://ifeve.com/java-memory-model/ 总结: Final 变量在并发 ...

  4. Java BigDecimal详解,提供了丰富的四舍五入规则

    java.math.BigDecimal类提供用于算术,刻度操作,舍入,比较,哈希算法和格式转换操作. toString()方法提供BigDecimal的规范表示.它使用户可以完全控制舍入行为. 提供 ...

  5. 35-BigDecimal详解

    详解 import java.math.BigDecimal; import java.math.BigInteger; import java.util.Scanner; public class ...

  6. spring AspectJ切入点语法详解 记录以便查阅

    AspectJ切入点语法详解 6.5.1  Spring AOP支持的AspectJ切入点指示符 切入点指示符用来指示切入点表达式目的,,在Spring AOP中目前只有执行方法这一个连接点,Spri ...

  7. spring AOP 之四:@AspectJ切入点标识符语法详解

    @AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...

  8. base大家族详解

    base大家族详解 */--> pre.src {background-color: #292b2e; color: #b2b2b2;} pre.src {background-color: # ...

  9. X509证书申请以及PKCS#10 详解

    一.证书颁发 1.单证书的签发 1) 用户填写信息注册(或者由RA的业务操作员注册用户). 2) 用户信息传递到RA. 3) RA审核通过. 4) 用户请求发证. 5) RA审核通过. 6) 用户签发 ...

随机推荐

  1. SpringAop源码情操陶冶-AspectJAwareAdvisorAutoProxyCreator

    本文将对SpringAop中如何为AspectJ切面类创建自动代理的过程作下简单的分析,阅读本文前需要对AOP的Spring相关解析有所了解,具体可见Spring源码情操陶冶-AOP之ConfigBe ...

  2. SpringMVC , Spring , MyBatis 文件上传

    学习一下文件上传下载,为图片上传做准备,感觉有一个世纪没玩过上传下载了,边敲代码边记录,请各路大神指教: 参考:http://blog.csdn.net/wjycgl/article/details/ ...

  3. 转 html5离线储存,application cache,manifest使用体验

    html5离线应用application cache 最近在APP里新增一个论坛模块,为了快速地完成,决定将整个论坛模块做成WEB APP,WEB APP最致命的就是用户体验问题,页面跳转和过多的请求 ...

  4. number 类型转换 符号

    function convert(sValue, sDataType) {   switch(sDataType) {      case “int”:          return parseIn ...

  5. 移动端1px 边框完整方案(四个方向)

    使用stylus(预处理) 需要一个函数接收两个参数 第一个需要在哪个方向出现边框 第二个边框颜色 $border1px(face,$color) 根据传入的方向属性,调整其他参数 l  左右方向 t ...

  6. JavaScript之数组五大迭代方法总结

    ES5定义了五个迭代方法,每个方法都接收两个参数:要在每一项上运行的函数和运行该函数的作用域对象(可选的),作用域对象将影响this的值.传入这些方法中的函数会接收三个参数:数组的项的值.该项在数组中 ...

  7. C#实现的Redis扩展项目(二次封装)

    Redis在当下的互联网项目当中的普及率我想都不用多说了,本文要介绍的這个项目是基于我对Redis理解程度的基础上写的一个公共类库项目,希望对各位童鞋有所帮助,也欢迎各位对我都内容提出更好的意见. 由 ...

  8. C#多线程和线程同步总结

    Thread 没有参数的线程启动 Thread newThread = new Thread(new ThreadStart(DoWork)); newThread.Start(); 有参数的线程启动 ...

  9. C#的Random到底该怎么使用

    先看代码: 在循环中,有的只NEW一个Random,有的每次都NEW 一个Random. Console.WriteLine("1.多个Random,默认随机种子,"); ; i ...

  10. 说说css中pt、px、em、rem都扮演了什么角色

    前段时间,终于仔仔细细的把pt.px.em.rem了解了一遍,简单整理了一下做个记录. pt.px.em.rem都是什么 pt单位名称为点(Point),绝对长度单位.现在网页中出现得很少甚至不出现, ...