Java min Function

The Java min Function is one of the Java Math Library functions, which is to return the Minimum or Smaller value from the given two arguments.

Java min Function Syntax

The basic syntax of the Math.min in Java Programming language is as shown below:

Math.min(data_type x, data_type y);

Java Programming provides four different functions to find the Minimum or Smaller value from the given two arguments. The following function will accept positive or negative integer value as the first and second argument and returns the Smaller value of integer type.

static int min(integer x, intger y); //Return Type is Integer

// In order to use in program: 
Math.min(int x, int y);

This Java min double function will accept the negative or positive double value and returns the Smaller value of double type.

static double min(double x, double y); //Return Type is double

// In order to use in program: 
Math.min(double x, double y);

It accepts positive or negative float value as the first and second arguments. It returns the Smaller value of float type.

static float min(float x, float y); //Return Type is float

// In order to use in program: 
Math.min(float x, float y);

The following function allows positive or negative long value and returns the Smaller value of Long type.

static long min(long x, long y); //Return Type is long

// In order to use in program: 
Math.long(long x, long y);

Java min Function Example

In this example, we use the Java Math.min function to find the Minimum or Smaller value from a different set of data types and display the output.

// Java Math.min Function
package MathFunctions;
public class MinMethod {
 public static void main(String[] args) {
 double b = 10.9666, c = 14.6674, d = -9.474, e = -14.9865;
 float g = 1.23f, h = 4.809f, i = -7.89f, j = -6.54f;
 long  k = 200, l = 400, m = -300, n = -100;
 
 System.out.println("Minimum value of Positive Integer: " + Math.min(10, 20));
 System.out.println("Minimum value of Negative Integer: " + Math.min(-20, -60));
 System.out.println("Minimum value of both Positive & Negative: " + Math.min(10, -20));
 
 System.out.println("\nMinimum value of Positive Double: " + Math.min(b, c));
 System.out.println("Minimum value of Negative Double: " + Math.min(d, e));
 System.out.println("Minimum value of both Positive & Negative: " + Math.min(b, e));
 
 System.out.println("\nMinimum value of Positive Float: " + Math.min(g, h));
 System.out.println("Minimum value of Negative Float: " + Math.min(i, j));
 System.out.println("Minimum value of both Positive & Negative: " + Math.min(h, i));
 
 System.out.println("\nMinimum value of Positive Number: " + Math.min(k, l));
 System.out.println("Minimum value of Negative Number: " + Math.min(m, n));
 System.out.println("Minimum value of both Positive & Negative: " + Math.min(k, n));
 }
}
Java min Function 1

First, we used the Java Math.min Function directly on both the Positive integer and negative integer values. The following statements will call the min method of integer type ( static int min(integer a, integer b) ) and find the smallest among the values.

System.out.println("Minimum value of Positive Integer: " + Math.min(10, 20));
System.out.println("Minimum value of Negative Integer: " + Math.min(-20, -60));
System.out.println("Minimum value of both Positive & Negative: " + Math.min(10, -20));

Next, We used the Math.min Function on variable b, c, d, and e (they belong to double type). The following statements will call the min method of double type ( static double min(double a, double b) ) and find the smallest among the values.

System.out.println("\nMinimum value of Positive Double: " + Math.min(b, c));
System.out.println("Minimum value of Negative Double: " + Math.min(d, e));
System.out.println("Minimum value of both Positive & Negative: " + Math.min(b, e));

Next, We used the Java min Function on variable g, h, i, and j (they belong to float type). The following statements will call the min method of float type ( static float min(float a, float b) ) and find the smallest among the values.

System.out.println("\nMinimum value of Positive Float: " + Math.min(g, h));
System.out.println("Minimum value of Negative Float: " + Math.min(i, j));
System.out.println("Minimum value of both Positive & Negative: " + Math.min(h, i));

Last, We used the min Math function on variable k, l, m, and n (they belong to long type). The following statements will call the min method of long type ( static long min(long a, long b) ) and find the smallest among the values.

System.out.println("\nMinimum value of Positive Number: " + Math.min(k, l));
System.out.println("Minimum value of Negative Number: " + Math.min(m, n));
System.out.println("Minimum value of both Positive & Negative: " + Math.min(k, n));