欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 【从零开始学习JAVA 】BigInteger

【从零开始学习JAVA 】BigInteger

2025/5/5 17:37:10 来源:https://blog.csdn.net/2301_80615512/article/details/146197387  浏览:    关键词:【从零开始学习JAVA 】BigInteger

前言:

本篇我们将介绍BigInteger这个比较实用一点的API,这个API在我们实际写项目中都是很实用的API,因此大家应该对这个API有更加熟练的掌握。

BigInterger: 

在Java中,整数类型int和long的取值范围是有限的,超出该范围的整数无法进行运算或表示。而在某些应用场景中,需要处理的数可能具有非常大的位数,此时可以使用Java中的BigInteger类。BigInteger是Java中的一个类,用于表示任意精度的整数。它可以处理比long更大范围的整数,支持加、减、乘和除等基本运算,还支持位运算、比较、判断奇偶性等操作。BigInteger对象是不可变的,因此一旦创建,就不能修改其值

以下是一个使用BigInteger类的简单示例。该示例演示了如何计算1到100的阶乘之和。

import java.math.BigInteger;public class BigIntDemo {public static void main(String[] args) {BigInteger result = BigInteger.ZERO;BigInteger fac = BigInteger.ONE;for (int i = 1; i <= 100; i++) {fac = fac.multiply(BigInteger.valueOf(i));result = result.add(fac);}System.out.println(result);}
}

输出:

9332621544394415268169923885626670049071596826438162146
8592963895217599993229915608941463976156518286253697920
827223758251185210916864000000000000000000000000

显而易见这超出了我们Java中基本数据类型的最大值

BigInteger.ZERO和BigInteger.ONE是BigInteger类的常量,分别表示值为0和1的BigInteger对象。它们的定义如下:

 
public static final BigInteger ZERO = new BigInteger(new int[0], 0);
public static final BigInteger ONE = valueOf(1);

 我们可以使用这两个常量来进行初始化操作,例如:

BigInteger x = BigInteger.ZERO;
BigInteger y = BigInteger.ONE;

这里我们将变量x和y,分别初始化为0和1的BigInteger对象。由于BigInteger对象是不可变的,因此这两个常量可以被多次使用,而不必担心其值被修改。

以上示例中,我们使用了BigInteger.ZERO和BigInteger.ONE常量来初始化了变量result和fac,包含了BigInteger.valueOf静态方法来创建一个大整数对象,这个创建方法接受一个long型的参数作为参数。这样就不必使用String类型的构造函数,而可以直接将一个整数包装在BigInteger对象中。在for循环中,我们使用了multiply方法来计算阶乘并将其加入到结果中。

BigInteger常见的方法: 

1. 构造函数和静态工厂方法

BigInteger类提供了多个构造函数和静态工厂方法(静态方法返回一个新的BigInteger对象),用于创建BigInteger对象。常用的有:

public BigInteger(String val):用字符串val创建一个BigInteger对象。
public BigInteger(int signum, byte[] magnitude):用数组magnitude表示的值创建一个BigInteger对象,其中signum指定值的正负性。
public static BigInteger valueOf(long val):用long类型的值创建一个新的BigInteger对象。
public static BigInteger probablePrime(int bitLength, Random rnd):返回一个bitLength位大的素数。

2. 算术运算方法

BigInteger类支持多种算术运算方法,包括加减乘除、求余等。常用的有:

public BigInteger add(BigInteger val):将当前BigInteger对象加上另一个BigInteger对象val。
public BigInteger subtract(BigInteger val):将当前BigInteger对象减去另一个BigInteger对象val。
public BigInteger multiply(BigInteger val):将当前BigInteger对象乘以另一个BigInteger对象val。
public BigInteger divide(BigInteger val):将当前BigInteger对象除以另一个BigInteger对象val。
public BigInteger mod(BigInteger val):求当前BigInteger对象对另一个BigInteger对象val取模。
public BigInteger pow(int exponent):求当前BigInteger对象的exponent次方

3. 比较方法

BigInteger类提供了多种比较方法,用于比较两个BigInteger对象的大小关系。常用的有:

public int compareTo(BigInteger val):比较当前BigInteger对象与另一个BigInteger对象val的大小关系,返回-1、0或1。
public boolean equals(Object obj):判断当前BigInteger对象是否等于另一个对象obj。
public int signum():返回当前BigInteger对象的符号,-1表示负数,0表示零,1表示正数。

4. 转换方法

BigInteger类提供了多种转换方法,用于将BigInteger对象转换为其他类型的值。常用的有:

public byte[] toByteArray():返回当前BigInteger对象的二进制表示,用byte数组表示。
public String toString()`:将当前BigInteger对象转换成字符串表示。
public String toString(int radix):将当前BigInteger对象转换成radix进制的字符串表示。
public int intValue()、public long longValue()、public float floatValue()、public double doubleValue():将当前BigInteger对象转换成对应的基本类型值。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词