BigInteger初识
为什么java里面要出现BigInteger类型呢?相信很多人有这个疑问,其实原因很简单,它可以表达更大范围的数值,远远比long表示的最大值还要大的多数。
在整数类型里面,long可以表达最大值,如下所示:
public class Test {
public static void main(String[] args) { System.out.println(Long.MAX_VALUE); } }
public class Test {
public static void main(String[] args) { BigInteger a= BigInteger.valueOf(L); BigInteger b= BigInteger.valueOf(L); BigInteger c=a.add(b); System.out.println(c.toString()); } }
BigInteger构造函数分析
BigInteger构造函数如下:

给构造函数传入不同的参数都会转变为BigInteger类型.具体使用可查看相应api.
BigInteger常用函数分析
- 比较函数:
int compareTo(BigInteger val)//比较大小
BigInteger min(BigInteger val)//返回较小的
BigInteger max(BigInteger val)//返回较大的
BigInteger经常遇到的问题
本文给大家说一下BigInteger的常见问题,总共有几个常见的问题,如下所示。
问题一:在java怎样将BigInteger类型的数据转成int类型的?
答案:BigInteger的intValue()可以获得int类型数值。
/ * Converts this BigInteger to an {@code int}. This * conversion is analogous to a * narrowing primitive conversion from {@code long} to * {@code int} as defined in section 5.1.3 of * The Java™ Language Specification: * if this BigInteger is too big to fit in an * {@code int}, only the low-order 32 bits are returned. * Note that this conversion can lose information about the * overall magnitude of the BigInteger value as well as return a * result with the opposite sign. * * @return this BigInteger converted to an {@code int}. * @see #intValueExact() */ public int intValue() { int result = 0; result = getInt(0); return result; }
问题二:在哪里可以查看BigInteger的代码实现?
答案:在jdk里面的java.math包下面就可以看到
问题三:在JAVA中BigInteger.ZERO是什么意思?
答案:在BigInteger内部定义的 一个代表 数字零 的常量,如下所示:
public static final BigInteger ZERO = new BigInteger(new int[0], 0);
问题四:在java中 有没有比BigInteger范围更大的?遇到比BigInteger范围更大的情况是不是只能用数组解决了?
问题五:java.math.BigInteger有位数限制么?比如long是2的64次方。
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/214587.html原文链接:https://javaforall.net
