Método BigInteger valueOf() en Java

El método java.math.BigInteger.valueOf(long value) devuelve un BigInteger cuyo valor es igual al valor de long pasado como parámetro. Este método es un método estático, por lo que no es necesario crear un objeto de la clase BigInteger para usar este método. Podemos llamar a esta función mediante el código BigInteger.valueOf (valor largo).

Sintaxis:

public static BigInteger valueOf(long val)

Parámetro: este método acepta un único valor de parámetro que es el valor del BigInteger que se va a crear.

Valor devuelto: este método devuelve el BigInteger cuyo valor es igual al valor pasado como parámetro.

Los siguientes programas ilustran el método valueOf (valor largo) de la clase BigInteger:

Ejemplo 1: aplicar valueOf() sin crear un objeto BigInteger.

// Java program to demonstrate valueOf() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Creating 2 BigInteger objects
        BigInteger b1, b2;
  
        // apply valueOf()
        b1 = BigInteger.valueOf(456782765);
        b2 = BigInteger.valueOf(12345543);
  
        // print result
        System.out.println("Value of BigInteger b1: " + b1);
        System.out.println("Value of BigInteger b2: " + b2);
    }
}
Producción:

Value of BigInteger b1: 456782765
Value of BigInteger b2: 12345543

Ejemplo 2: aplicar valueOf() creando un objeto BigInteger.

// Java program to demonstrate valueOf() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Creating BigInteger objects
        BigInteger b1, b2, b3;
  
        // create bigInteger with some value
        b1 = new BigInteger("532721");
  
        // apply valueOf()
        b2 = b1.valueOf(234567898);
        b3 = b2.valueOf(98765432);
  
        // print result
        System.out.println("Value of BigInteger 1: " + b2);
        System.out.println("Value of BigInteger 2: " + b3);
    }
}
Producción:

Value of BigInteger 1: 234567898
Value of BigInteger 2: 98765432

Referencia: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#valueOf(long)

Publicación traducida automáticamente

Artículo escrito por AmanSingh2210 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *