Max Value From BigInteger in Java

Rupam Yadav Oct 12, 2023
  1. Find Maximum Value in BigInteger With Positive Values in Java
  2. Find Maximum Value in BigInteger With Negative Values in Java
  3. Find Maximum Value in BigInteger With Same Values in Java
Max Value From BigInteger in Java

This tutorial shows how to get a maximum value from BigInteger data type values in Java.

As the name suggests, BigInteger is commonly used to store large integers that a standard primitive int type cannot hold because of its memory limitations.

Find Maximum Value in BigInteger With Positive Values in Java

The following example shows how we get the maximum value between two BigInteger variables that contain positive values. We create two instances of the BigInteger class and pass different numbers as a string in the constructor.

To get the maximum value from both of these objects, we use the max() method in the BigInteger class itself and take an instance of BigInteger as an argument.

After we do bigInteger1.max(bigInteger2), it returns a BigInteger with the largest value of the previous objects we compared.

Now we print the getMaxValue and get the larger value in the output.

import java.math.BigInteger;

public class ExampleClass2 {
  public static void main(String[] args) {
    BigInteger bigInteger1 = new BigInteger("2021");
    BigInteger bigInteger2 = new BigInteger("200");

    BigInteger getMaxValue = bigInteger1.max(bigInteger2);

    System.out.println(getMaxValue);
  }
}

Output:

2021

Find Maximum Value in BigInteger With Negative Values in Java

Now we check if the max() method can handle negative values or not. We create two BigInteger objects, and in the first constructor, we pass a positive value, and in the second constructor, we pass a negative value.

When we call the max() method and pass the objects, we get the correct output that is the larger value.

import java.math.BigInteger;

public class ExampleClass2 {
  public static void main(String[] args) {
    BigInteger bigInteger1 = new BigInteger("20003");
    BigInteger bigInteger2 = new BigInteger("-20010");

    BigInteger getMaxValue = bigInteger1.max(bigInteger2);

    System.out.println(getMaxValue);
  }
}

Output:

20003

Find Maximum Value in BigInteger With Same Values in Java

In this example, we use the same values for both the BigInteger objects and the output of the max() method is returned as the same value, which means that it returns either of the value as they are the same.

import java.math.BigInteger;

public class ExampleClass2 {
  public static void main(String[] args) {
    BigInteger bigInteger1 = new BigInteger("4065");
    BigInteger bigInteger2 = new BigInteger("4065");

    BigInteger getMaxValue = bigInteger1.max(bigInteger2);

    System.out.println(getMaxValue);
  }
}

Output:

4065
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java BigInteger