Java.math.BigInteger.valueOf() Method



Description

The java.math.BigInteger.valueOf(long val) returns a BigInteger whose value is equal to that of the specified long. This "static factory method" is provided in preference to a (long) constructor because it allows for reuse of frequently used BigIntegers.

Declaration

Following is the declaration for java.math.BigInteger.valueOf() method.

public static BigInteger valueOf(long val)

Parameters

val − Value of the BigInteger to return.

Return Value

This method returns a BigInteger with the specified value.

Exception

NA

Example

The following example shows the usage of math.BigInteger.valueOf() method.

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

      // create a BigInteger object
      BigInteger bi;

      // create and assign value to Long object
      Long l = new Long(123456789L);

      // assign the biginteger value of l to bi
      // static method is called using class name
      bi = BigInteger.valueOf(l);

      String str = "BigIntger value of Long " + l + " is " +bi;

      // print bi value
      System.out.println( str );
   }
}

Let us compile and run the above program, this will produce the following result −

BigIntger value of Long 123456789 is 123456789
java_math_biginteger.htm
Advertisements