LogIn
I don't have account.

Java Math min Method

DevSniper

136 Views
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) 
Java Math.min is a inbuild static method. it returns lowest value of given two arguments. It is present in Java.lang.Math. Java.lang.Math provides set of methods to perform mathematical operations.
Example 1
Copy
public class Program{

     public static void main(String []args){
        int x=5;
        int y=10;
        System.out.println("min value of "+x+" and "+y+" is =  "+ Math.min(x,y));
        x=5;
        y=-10;
        System.out.println("min value of "+x+" and "+y+" is =  "+ Math.min(x,y));
        x=-5;
        y=-10;
        System.out.println("min value of "+x+" and "+y+" is =  "+ Math.min(x,y));
     }
}
min value of 5 and 10 is =  5
min value of 5 and -10 is =  -10
min value of -5 and -10 is =  -10
Example 2
Copy
public class Program{

     public static void main(String []args){
        double x=9.25;
        double y=9.50;
        System.out.println("Min value of "+x+" and "+y+" is =  "+ Math.min(x,y));
        y=-y;
        System.out.println("Min value of "+x+" and "+y+" is =  "+ Math.min(x,y));
        x=-x;
        System.out.println("Min value of "+x+" and "+y+" is =  "+ Math.min(x,y));
     }
}
Min value of 9.25 and 9.5 is =  9.25
Min value of 9.25 and -9.5 is =  -9.5
Min value of -9.25 and -9.5 is =  -9.5
Example 3
Copy
public class Program{

     public static void main(String []args){
        long x=10000000000L;
        long y=10000000002L;
        System.out.println("Min value of "+x+" and "+y+" is =  "+ Math.min(x,y));
        y=-y;
        System.out.println("Min value of "+x+" and "+y+" is =  "+ Math.min(x,y));
        x=-x;
        System.out.println("Min value of "+x+" and "+y+" is =  "+ Math.min(x,y));
     }
}
Min value of 10000000000 and 10000000002 is =  10000000000
Min value of 10000000000 and -10000000002 is =  -10000000002
Min value of -10000000000 and -10000000002 is =  -10000000002
Example 4
Copy
public class Program{

     public static void main(String []args){
        float x=9.25f;
        float y=9.50f;
        System.out.println("Min value of "+x+" and "+y+" is = "+ Math.min(x,y));
        y=-y;
        System.out.println("Min value of "+x+" and "+y+" is = "+ Math.min(x,y));
        x=-x;
        System.out.println("Min value of "+x+" and "+y+" is = "+ Math.min(x,y));
     }
}
Min value of 9.25 and 9.5 is = 9.25
Min value of 9.25 and -9.5 is = -9.5
Min value of -9.25 and -9.5 is = -9.5