Java min() Method: Usage and Examples

Rumman Ansari   Software Engineer   2024-07-04 04:06:19   6367  Share
Subject Syllabus DetailsSubject Details 1 Program
☰ TContent
☰Fullscreen

Table of Content:

Description

On this tutorial, we will be showing a java example on how to use the min() method of Math Class. The min() returns which of the two method argument has the lowest value numerically. This method is overloaded such that it handles all primitive data type. Here are the overloaded methods:

The method gives the smaller of the two arguments. The argument can be int, float, long, double.

Syntax

This method has the following variants ?

double min(double arg1, double arg2)
float min(float arg1, float arg2)
int min(int arg1, int arg2)
long min(long arg1, long arg2)

The overloaded methods are basically the same, it’s just that they deal with different data type either int, long, float, and double

Most of the methods of the Math class is static and the min() method is no exception. Thus don’t forget that in order to call this method, you don’t have to create a new object. Use the method in the format Math.min(a).

 

Parameters

Here is the detail of parameters ?

  • This method accepts any primitive data type as a parameter.

Return Value

  • This method returns the smaller of the two arguments.

Example

public class MathMinMethod {

   public static void main(String args[]) {
      System.out.println(Math.min(52.123, 82.456));
      System.out.println(Math.min(27.12, 23.0));
   }
}

Output

This will produce the following result ?

52.123
23.0
Press any key to continue . . .

Example

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

import java.util.Scanner;

/*
 * This example source code demonstrates the use of
 * min() method of Math class
 */

public class MathMinExample {

	public static void main(String[] args) {

		// Ask for user input
		System.out.print("Enter first value: ");

		// use scanner to read the console input
		Scanner scan = new Scanner(System.in);

		// Assign the 1st input to String variable
		String value1 = scan.nextLine();

		// ask for the second input
		System.out.print("Enter second value: ");

		// Assign the 2nd input to String variable
		String value2 = scan.nextLine();

		// close the scanner object
		scan.close();

		// convert the values to int
		int a = Integer.parseInt(value1);
		int b = Integer.parseInt(value2);

		// get the result of min method
		int result = Math.min(a,b);
		System.out.println("Lowest value: "+result);
	}

}

Output

The above java example source code demonstrates the use of min() method of Math class. We simply ask for 2 user input and we use the Scanner class to parse it. Since we have used the nextLine() method to get the console value which is having a return data type of String thus we have used the Integer.parseInt to transform it into int. Alternatively if the requirements is to use long then you must use the Long.ParseLong() instead, Double.parseDouble() for double, and Float.parseFloat() for float. This conversion is required because the argument for min() method only accepts either int, long, double or float.

This will produce the following result ?

Enter first value: 200
Enter second value: 100
Lowest value: 100
Press any key to continue . . .

Find minimum of two numbers using Math.min

/*
  Find minimum of two numbers using Math.min
  This java example shows how to find minimum of two int, float,
  double or long numbers using min method of Java Math class.
*/

public class FindMinimumOfTwoNumbersExample {

  public static void main(String[] args) {

    /*
     * To find minimum of two int values, use
     * static int min(int a, int b) method of Math class.
     */

    System.out.println(Math.min(34,45));

    /*
     * To find minimum of two float values, use
     * static float min(float f1, float f2) method of Math class.
     */
     System.out.println(Math.min(43.34f, 23.34f));

    /*
     * To find minimum of two double values, use
     * static double min(double d2, double d2) method of Math class.
     */
     System.out.println(Math.min(4324.334, 3987.342));

    /*
     * To find minimum of two long values, use
     * static long min(long l1, long l2) method of Math class.
     */

     System.out.println(Math.min(48092840,4230843));
  }
}

Output

This will produce the following result ?

34
23.34
3987.342
4230843
Press any key to continue . . .