Java Math min() method example

In this tutorial we will learn about java math min() method . Java Math.min() method takes as parameter two arguments and returns he smaller of the two values . If the both arguments have the same value, the result is that same value.

Following is the syntax of java Math.min() method.  We can see the datatype of return value is based on the datatype of arguments.

public static int min(int a, int b) 
public static double min(double a, double b) 
public static long min(long a, long b) 
public static float min(float a, float b) 

Parameters :

a: first argument
b: second argument

Return : It’s returns minimum of two numbers.

Now We will see java math.min() method in detail with the help of examples. We will  discuss different approaches of min function in java .

Example 1 – Java Math.min() method – arguments are int

In the example, we will pass two integers as arguments to min function in java and find the smallest of the two integer numbers.

public class JavaMathMin {
	
	public static void main(String[] args) {
		
	int a = 40;
        int b = 30; 
        //Math.min(int, int)
        int minimum = Math.min(a, b);
        System.out.println("minimum value of "+ a +" and "+b+" is : "+minimum);
        
	}

}

Output:

minimum value of 40 and 30 is : 30

Example 2 – Java Math.min() method – positive and negative value as argument

In this example, we will use positive and one negative integer as arguments to java Math.min() method . If we provide positive and negative value as argument, then this method will return negative argument.

public class JavaMathMin1 {
	
	public static void main(String[] args) {
		int a = -40;
        int b = 30; 
        //Math.min(int, int)
        int minimum = Math.min(a, b);
        System.out.println("minimum value of "+ a +" and "+b+" is : "+minimum);
        
	}

}

Output :

minimum value of -40 and 30 is : -40

Example 3 – Math.min() method In Java – arguments are negative

In this example, we will use negative integers as arguments to min function in java . If we use both negative values as argument, then number with the higher magnitude is returned as result.

public class JavaMathMin2 {
	
	public static void main(String[] args) {
		
	int a = -40;
        int b = -30; 
        //Math.min(int, int)
        int minimum = Math.min(a, b);
        System.out.println("minimum value of "+ a +" and "+b+" is : "+minimum);    
        
	}

}

Output:

minimum value of -40 and -30 is : -40

Example 4 – Java Math.min() method – arguments are double

In the example, we will provide two double as arguments to min function in java and find the smallest of the two double numbers.

public class JavaMathMin3 {
	
	public static void main(String[] args) {
		
		 double a = 10.92875;
	     double b = 892020.92991923078; 
        
        //Math.min(double, double)
	     double minimum = Math.min(a, b);
        
        System.out.println("minimum value of "+ a +" and "+b+" is : "+minimum);
        
	}

}

Output:

minimum value of 10.92875 and 892020.9299192308 is : 10.92875

Example 5 – Java Math.min(x,y) – x or y as NaN

In the example, we will provide one argument as NaN to java min function. If any of the arguments to min() method is NaN, then It will return NaN.

public class JavaMathMin4 {
	
	public static void main(String[] args) {
		
		 double a = 10.9985;
	     double b = Double.NaN; 
        
        //Math.min(double, double)
	     double minimum = Math.min(a, b);
        
        System.out.println("minimum value of "+ a +" and "+b+" is : "+minimum);
		
	}

}

Output:

minimum value of 10.9985 and NaN is : NaN

Example 6 – Java Math.min(int, float)

If we will provide different datatype value as arguments to min() method, then the lower datatype is promoted to higher datatype and smallest value returned .

public class JavaMathMin5 {

	public static void main(String[] args) {
		
		int a = 40;
        float b = 30.2F; 
        
        float  minimum = Math.min(a, b);
        System.out.println("minimum value of "+ a +" and "+b+" is : "+minimum);
        
	}
}

Output:

minimum value of 40 and 30.2 is : 30.2

In this tutorial we have learned java Math.min() method .We have explained java min function with examples. you can see more java program for practice .

Leave a Reply

Your email address will not be published. Required fields are marked *

36 − 29 =