在函数中传递BigInteger
对象可以通过以下几种方式:
BigInteger
对象作为参数传递给函数。import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
BigInteger number = new BigInteger("123456789");
printBigInteger(number);
}
public static void printBigInteger(BigInteger number) {
System.out.println(number);
}
}
BigInteger
对象封装在一个类中,然后将该类的实例作为参数传递给函数。import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
BigIntegerWrapper wrapper = new BigIntegerWrapper(new BigInteger("123456789"));
printBigInteger(wrapper);
}
public static void printBigInteger(BigIntegerWrapper wrapper) {
System.out.println(wrapper.getNumber());
}
}
class BigIntegerWrapper {
private BigInteger number;
public BigIntegerWrapper(BigInteger number) {
this.number = number;
}
public BigInteger getNumber() {
return number;
}
}
BigInteger
对象,并将值赋给它。import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
BigInteger number = new BigInteger("123456789");
addOneToBigInteger(number);
}
public static void addOneToBigInteger(BigInteger number) {
number = number.add(BigInteger.ONE);
System.out.println(number);
}
}
无论选择哪种方式,都可以在函数中传递BigInteger
对象。
上一篇:BigInteger性能