Java BigInteger Class | valueOf() 的方法和示例
写在前面:
在这里,我们将学习BigInteger class valueOf()方法及其语法和示例。
BigInteger Class valueOf() method
- valueOf() method 在 java.math package可用的
- valueOf() method 用于将给定的long值表示为一个BigInteger。
- valueOf() method 是静态方法,它是可以用类名访问的,如果我们试图用类对象访问该方法,那么我们不会得到错误。
- valueOf() method 在表示long值时不抛出异常。
语法
public BigInteger valueOf(long val);
参数
long val – 表示以BigInteger形式返回的long值。
返回值
这个方法的返回类型是BigInteger,它返回给定long值的BigInteger。
示例
// Java program to demonstrate the example
// of BigInteger valueOf(long val) method of BigInteger
import java.math.*;
public class ValueOfOfBI {
public static void main(String args[]) {
// Instantiates a variable l_val
long l_val = 125487312456l;
// Here, this method valueOf(long) is used
// to convert the given long value into
// a BigInteger and store it in a variable
// named value_of
BigInteger value_of = BigInteger.valueOf(l_val);
System.out.println("l_val: " + l_val);
System.out.println("valueOf(long): ");
// Display value_of
System.out.println("BigInteger.valueOf(l_val): " + value_of);
}
}
输出
l_val: 125487312456
valueOf(long):
BigInteger.valueOf(l_val): 125487312456