Java min Function

The Java min Function is one of the 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 min 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 the 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);

Similarly, this Java math min function accepts the negative or positive values of double, float, and long datatypes and returns the Smaller value of the given datatype.

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

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

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

Java min Function Example

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

First, we used it directly on both positive and negative integer values. The first three statements will call the integer type static int min(integer a, integer b) method and find the smallest among the values.

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

Next, We used the Function on variables g, h, i, and j (they belong to float type). The next three System.out.println statements will call the float type method ( static float min(float a, float b) ) and find the smallest among the values.

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

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