java.math.BigInteger max(BigInteger val)
Description
There is a similar method available on BigInteger class which is the compareTo() method. The max method is a little bit different because on the compareTo() method we have to interpret the result while on the max method, the biggest BigInteger is already returned.
See also:
Method Syntax
public BigInteger max(BigInteger val)
Method Argument
Data Type | Parameter | Description |
---|---|---|
BigInteger | val | value with which the maximum is to be computed. |
Method Returns
The max(BigInteger val) method returns the BigInteger whose value is the greater between this BigInteger and val. If they are equal, either may be returned.
Compatibility
Requires Java 1.1 and up
Java BigInteger max(BigInteger val) Example
Below is a java code demonstrates the use of max(BigInteger val) method of BigInteger class. The example presented might be simple however it shows the behavior of the max() method.
package com.javatutorialhq.java.examples; import java.math.BigInteger; import java.util.Scanner; /* * A java example source code to demonstrate * the use of max() method of BigInteger class */ public class BigIntegerMaxExample { public static void main(String[] args) { // ask for two input System.out.print("Enter the first number:"); Scanner s = new Scanner(System.in); String firstInput = s.nextLine(); System.out.print("Enter the second number:"); String secondInput = s.nextLine(); s.close(); // convert the string input to BigInteger BigInteger val1 = new BigInteger(firstInput); BigInteger val2 = new BigInteger(secondInput); // get the maximum value between the two BigInteger BigInteger result = val1.max(val2); System.out.println("The result is "+result); } }
This example is a lot simpler than it looks. We simply ask the user for two inputs and convert these into BigInteger. We then get and print what number is higher between the two inputs.