java.math.BigInteger min(BigInteger val)

Description

On this document we will be showing a java example on how to use the min(BigInteger val) method of BigInteger Class. Basically this method provides mechanism to get which is numerically lower between this BigInteger and the val method argument. Whichever is lower will be returned by this method.

There is a similar method available on BigInteger class which is the compareTo() method. The min() method is a little bit different because on the compareTo() method we have to interpret the result while on the min() method, the smallest BigInteger will be returned.

See also:

Method Syntax

public BigInteger min(BigInteger val)

Method Argument

Data Type Parameter Description
BigInteger val value with which the minimum is to be computed.

Method Returns

The min(BigInteger val) method returns the BigInteger whose value is the minimum between this  BigInteger and val. If they are equal, either may be returned.

Compatibility

Requires Java 1.1 and up

Java BigInteger min(BigInteger val) Example

Below is a java code demonstrates the use of min(BigInteger val) method of BigInteger class. The example presented might be simple however it shows the behavior of the min() method.

package com.javatutorialhq.java.examples;

import java.math.BigInteger;
import java.util.Scanner;

/*
 * A java example source code to demonstrate
 * the use of min() method of BigInteger class
 */

public class BigIntegerMinExample {

	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 minimum value between the two BigInteger
		BigInteger result = val1.min(val2);
		System.out.println("The lowest 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 lower between the two inputs.

Sample Output

Below is the sample output when you run the above example.

BigInteger min() example output